name()- Takes zero or one node-set arguments and returns the name of the node in prefix:localpart format : name « XSLT stylesheet « XML Tutorial






File: Data.xml

<list>
 <freezer>
  <element>peas</element>
  <element>green beans</element>
  <element>pot pie</element>
  <element>ice cream</element>
 </freezer>
 <bakery>
  <element>rolls</element>
  <element>jelly doughnuts</element>
  <element>bread</element>
 </bakery>
 <produce>
  <element>bananas</element>
  <element>kumquats</element>
  <element>apples</element>
 </produce>
</list>

File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="list">
    <xsl:copy>
      <xsl:apply-templates select="*">
        <xsl:sort select="name()" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates select="element">
        <xsl:sort />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="element">
    <xsl:copy-of select="." />
  </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<list>
   <bakery>
      <element>bread</element>
      <element>jelly doughnuts</element>
      <element>rolls</element>
   </bakery>
   <freezer>
      <element>green beans</element>
      <element>ice cream</element>
      <element>peas</element>
      <element>pot pie</element>
   </freezer>
   <produce>
      <element>apples</element>
      <element>bananas</element>
      <element>kumquats</element>
   </produce>
</list>








5.62.name
5.62.1.name()- Takes zero or one node-set arguments and returns the name of the node in prefix:localpart format
5.62.2.name() function
5.62.3.get the name of currently selected element with name function
5.62.4.prints names of all elements used in the document
5.62.5.An example of use of function name()