juergen d
juergen d

Reputation: 204766

XSL Transformation: Get element by ID from variable

I am parsing a XML file with different elements to build a CSV. I iterate through persons like this

<xsl:template match="/root/persons/person"> 
     <xsl:for-each select=".">
        <xsl:value-of select="name" />
        <xsl:text>;</xsl:text>

        <xsl:variable name="refId">
           <xsl:value-of select="role/@refId"/>
        </xsl:variable>

        <!-- This is the problematic part -->
        <xsl:value-of select="/root/roles/role[@id='$refId']"/>

        <xsl:text>&#10;</xsl:text>
     </xsl:for-each>
</xsl:template>

In my person element I have a refId that refers to the ID of a role element. How can I get the role name with the refId loaded into a variable like above?

Upvotes: 0

Views: 540

Answers (1)

AntonH
AntonH

Reputation: 6437

Try using this:

<xsl:value-of select="/root/roles/role[@id=string($refId)]"/>

I'm supposing that your path is correct, since we can't see your XML to validate.

Upvotes: 1

Related Questions