Help - Conditionally Appending to Value using XSL?

J

jjouett

I have XML that looks like the following:

<top>
<heart>
<user>bob</user>
<user>tim-o</user>
<user>joe-i</user>
<user>harvey</user>
</heart>
</top>

and I need to have it look like the following:

<top>
<heart>
<user>bob</user>
<user>tim-out</user>
<user>joe-in</user>
<user>harvey</user>
</heart>
</top>

In other words, I need to conditionally append text based on the data
value (append 'n' if it ends in '-i', append 'ut' if it ends in '-o,
otherwise do nothing). So far, I haven't come across an approach to
accomplish this, but I am pretty much an XSLT newbie. Does anyone have
suggestions on how to accomplish this?

Thanks in advance
 
J

Joris Gillis

Hi,

Tempore 19:40:48 said:
I need to conditionally append text based on the data
value (append 'n' if it ends in '-i', append 'ut' if it ends in '-o,
otherwise do nothing). So far, I haven't come across an approach to
accomplish this, but I am pretty much an XSLT newbie. Does anyone have
suggestions on how to accomplish this?

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>

<xsl:template match="text()[substring(.,string-length(.)-1,2)='-i']">
<xsl:value-of select="."/>n<xsl:text/>
</xsl:template>

<xsl:template match="text()[substring(.,string-length(.)-1,2)='-o']">
<xsl:value-of select="."/>ut<xsl:text/>
</xsl:template>

</xsl:stylesheet>

regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top