1

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.