Groups | Blog | Home
all groups > dotnet xml > july 2004 >

dotnet xml : How do I write a string in the destination element depending on what the string is in the source, using XSLT/XPath? (Like a switch statement.)


Joakim Olesen
7/20/2004 5:23:51 PM
Is there something like a switch statement in XPath? What I want to do is
this:

In the source there is an element called "member" with either the value "0"
or "1". In the destination element (also called "member") the value should
be "No" or "Yes" (Where "0" corresponds to "No" and "1" to "Yes"). How do I
do this?

Oleg Tkachenko [MVP]
7/20/2004 6:58:12 PM
[quoted text, click to view]

Usual way:

<xsl:choose>
<xsl:when test="member = 0">No</xsl:when>
<xsl:when test="member = 1">Yes</xsl:when>
</xsl:choose>

Tricky way:

<xsl:value-of select="concat(
substring('No', (member=1)*string-length('No')+1),
substring('Yes', (member=0)*string-length('Yes')+1)
)"/>

Cool way:

<xsl:value-of select="msxsl:node-set('No')[current()/member=0]) |
msxsl:node-set('Yes')[current()/member=1])"/>

Lookup table way (better when you have lots of options):

<xsl:stylesheet xmlns:foo="bar"...>
<foo:table>
<item from="0" to="No"/>
<item from="1" to="Yes"/>
</foo:table>

....
<xsl:value-of
select="document('')/foo:table/item[@from=current()/member]/@to"/>

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button