1

I have xml as follows

   <pin>
        <securityProfile>
            <userName>userName</userName>
        </securityProfile>
    </pin>

Expected output

     <user_id>
          <userName>userName</userName>
    </user_id>

xslt

<xsl:template match="/pin">
    <user_id>
        <xsl:apply-templates select="securityProfile/userName"/>
    </user_id>      
</xsl:template>

<xsl:template match="userName">
    ..........................
</xsl:template> 

If i change <xsl:template match="/pin"> to <xsl:template match="/"> , then it is not working.What might be the reason .here pin is the root element.

Thanks in advance..

PSR
  • 39,804
  • 41
  • 111
  • 151
  • [Here is an excellent explanation of how `/` works](http://stackoverflow.com/questions/3127108/xsl-xsltemplate-match) for the _root node_ (usually the document node, but can be absent) and how it differs from `/somename` for selecting the _root element(s)_, usually one, and colloquially considered the root by many, but they aren't the same and the difference is subtle, but important. – Abel Oct 07 '15 at 14:37

1 Answers1

1

If i change <xsl:template match="/pin"> to <xsl:template match="/"> , then it is not working.

If you change that, you also need to change:

<xsl:apply-templates select="securityProfile/userName"/>

to:

<xsl:apply-templates select="pin/securityProfile/userName"/>

Otherwise you're applying templates to non-existing nodes.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Here root element is pin ?Then why i need to give again pin here ? – PSR Oct 07 '15 at 14:13
  • You're not giving it *again*. Your template's match pattern establishes the `/` *root node* as the current context. From this context, the relative path to `userName` is `pin/securityProfile/userName`. – michael.hor257k Oct 07 '15 at 14:19
  • I thought that , , both are same.Am i right or wrong ? – PSR Oct 07 '15 at 14:27
  • 1
    You are wrong. `` matches the **root node**. `` matches the **root element**. The root element is a child of the root node. -- Note that different standards may use different terms to describe these two nodes; see: http://www.dpawson.co.uk/xsl/sect2/root.html – michael.hor257k Oct 07 '15 at 14:30
  • Thank you .I got it now. – PSR Oct 07 '15 at 14:40