name() xslt function

Name

1. NAME of the XML element
2. How to Transform an element name to a text value
3. Transform an element name to a text value
4. Output content

1.

NAME of the XML element

Ken Holman

 Response by example.
Input file

<?xml version="1.0"?>
<test xmlns:crane="http://www.CraneSoftwrights.com/s/">
  <crane:test1 attr1="attr">
    <!--a comment-->
    <test2 crane:attr2="attr"/>
  </crane:test1>
  <?pitest here?>
</test>

Stylesheet.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Tranform"     version="1.0">

<xsl:template match="/">
  <xsl:for-each select="//*|//@*|//pi()">

name(.):      <xsl:value-of select="name(.)"/>
local-part(.): <xsl:value-of select="local-part(.)"/>
prefix:        <xsl:value-of select="substring-before
                     ( name(.), ':' )"/>
namespace(.):  <xsl:value-of select="namespace(.)"/>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

output file


name(.):      test
local-part(.): test
prefix:        
namespace(.):  

name(.):      crane:test1
local-part(.): test1
prefix:        crane
namespace(.):  http://www.CraneSoftwrights.com/s/

name(.):      attr1
local-part(.): attr1
prefix:        
namespace(.):  

name(.):      test2
local-part(.): test2
prefix:        
namespace(.):  

name(.):      crane:attr2
local-part(.): attr2
prefix:        crane
namespace(.):  http://www.CraneSoftwrights.com/s/

name(.):      pitest
local-part(.): pitest
prefix:        
namespace(.):  

            

2.

How to Transform an element name to a text value

Eric van der Vlist

You can use the name() function (documented in the XPATH specs).

3.

Transform an element name to a text value

Larry Mason

Q; Expansion: What I want to do is log which element had the error and
the value that was erroneous.

The output looks like this:
<P>Error: <Product/>=Coat<P>
or
<P>Error: <Product>=Coat</Product><P>

What I want is this:
<P>Error: Product=Coat<P>
You can use the name() function (documented in the XPATH specs)
to get access to the name;

 try <xsl:value-of select="name(.)"/> 

            

4.

Output content

Mike Kay

Given:
<stocks>
  <stock>
    <symbol>xyz</symbol>
    <price>123</price>
  </stock>
  <stock>
    <symbol>abc</symbol>
    <price>234</price>
  </stock>
  <stock>
    <symbol>pqr</symbol>
    <price>345</price>
  </stock>
</stocks>

I want to write an XSL for this which will read the tags and their
value dynamically.
Something like:


to generate:

  symbol = xyz ; price = 123 ; symbol = abc ; price = 234
  
Try:

<xsl:template match="/">
   <xsl:for-each select="stocks/stock">

<xsl:for-each select="*"/>
     <xsl:value-of select="name()"/>
      <xsl:text> = </xsl:text>
      <xsl:value-of select=".">
<xsl:text> ; </xsl:text>
</xsl:for-each>

    </xsl:for-each>
</xsl:template>