fishiefishie
fishiefishie

Reputation: 31

Get current node's local-name in XSL

Here's the structure of my XML

<FileRoot>
    <UserSet1>
         <User>
            <FirstName></FirstName>
            <LastName></LastName>
         </User>
         <User>
            <FirstName></FirstName>
            <LastName></LastName>
         </User>
         ...
    </UserSet1>
    <InactiveUsers>
         <User>
            <FirstName></FirstName>
            <LastName></LastName>
         </User>
         <User>
            <FirstName></FirstName>
            <LastName></LastName>
         </User>
         ...
    </InactiveUsers>
</FileRoot>

In my XSL template

<xsl:template match="/*/*">
   <File>
      <xsl attribute name="Name">
          <xsl:value-of select="local-name(/*/*)"/>
      </xsl:attribute>
   </File>
</xsl>

After transforming, for both UserSet1 and InactiveUsers, gave me "UserSet1". The expected results should be "UserSet1" for UserSet1, and "InactiveUsers" for InactiveUsers. How do I correctly retrieve the value?

Thanks

Upvotes: 0

Views: 9415

Answers (1)

markusk
markusk

Reputation: 6667

/*/* is an absolute path, so local-name(/*/*) will always return the local name for the first node in the entire document that matches that absolute path. It looks like you want the local name of the current node. In that case, use local-name() instead. When no argument is specified, the current context node is used.

Also, you could use an attribute value template instead of xsl:attribute, as follows:

<xsl:template match="/*/*">
   <File Name="{local-name()}"/>
</xsl>

Upvotes: 2

Related Questions