get attribute name : Attribute « XSLT stylesheet « XML






get attribute name


File: Data.xml

<poem year="1667" type="epic">
  <verse>line 3</verse>
  <verse>line 4</verse>
</poem>

File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />

  <xsl:template match="poem">
    <xsl:element name="{@type}">
      <author>John</author>
      <year>
        <xsl:value-of select="@year" />
      </year>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

  <xsl:template match="verse">
    <verse>
      <xsl:apply-templates />
    </verse>
  </xsl:template>

  
</xsl:stylesheet>

Output:

<epic><author>John</author><year>1667</year>
  <verse>line 3</verse>
  <verse>line 4</verse>
</epic>

 








Related examples in the same category

1.compare attribute value
2.Get attribute from different level
3.List the attribute names and values
4.if there is an attribute
5.Get value of attribute with @
6.select node by attribute value
7.Set attribute value in tranformation
8.attribute omitted