Reputation: 153
I have the following snipset of code
<xsl:variable name="cId" value="c001" />
<clients>
<c001>Mario</c001>
<c002>Luigi</c002>
</clients>
And Based on variable's value, I need to select the correct element under clients.
For Example. variable cId is assigned with value c001. Is there a way for me to select the value of c001 using XPATH or XSLT?
I can do it this way, but seems like for-loop is a little bit over kill
<xsl:variable name="cId" value="c001" />
<xsl:for-each select="/clients/*">
<xsl:variable name="cNode" select="local-name()"/>
<xsl:if test="$cNode = $cId">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
The code above will return "Mario"
Thanks for all the help.
Upvotes: 1
Views: 5409
Reputation: 243599
This question is not very clear, but you probably want something like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<my:catalog>
<catalog>
<client>
<clientid>c001</clientid>
<clientid>c003</clientid>
<clientid>c004</clientid>
<clientid>c005</clientid>
</client>
</catalog>
</my:catalog>
<xsl:variable name="vCat"
select="document('')/*/my:catalog"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"/clients/*
[name() = $vCat/catalog/client/clientid]
/text()
"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<clients>
<c001>Stars Company</c001>
<c002>Bold Unregistered</c002>
</clients>
the wanted result is produced:
Stars Company
Upvotes: 1