Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Axes (6/8) >

Attribute axis

The "attribute::" axis of an element contains all attributes of the element. If the element is without attributes or if the node is not an element then the attribute axis is empty.

It is important to realize that attributes of an element are not ordered, an XML parser is not required to assign to the first attribute following element name position "1", to the second "2", and so on as in this example. If other processor was used the result could be different. Therefore, correct functioning of your stylesheets should never rely on position of attributes.

XSLT

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

            <xsl:template  match="/">
                  <xxx>
                        <xsl:apply-templates  select="/aaa/@*"/>
                  </xxx>
                  <yyy>
                        <xsl:apply-templates  select="/aaa/bbb/@*"/>
                  </yyy>
            </xsl:template>

            <xsl:template  match="@*">
                  <zzz  name="{name()}"
                              position="{position()}"
                              parent="{name(parent::*)}"/>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa  p="1"
                  r="2"
                  s="3">
            <bbb  s="x"
                        r="y"
                        p="z"/>
      </aaa>
Output

      <xxx>
            <zzz  name="p"
                        position="1"
                        parent="aaa"/>
            <zzz  name="r"
                        position="2"
                        parent="aaa"/>
            <zzz  name="s"
                        position="3"
                        parent="aaa"/>
      </xxx>
      <yyy>
            <zzz  name="s"
                        position="1"
                        parent="bbb"/>
            <zzz  name="r"
                        position="2"
                        parent="bbb"/>
            <zzz  name="p"
                        position="3"
                        parent="bbb"/>
      </yyy>


Previous chapter: Sorting
Next chapter: Named-Templates
Previous page: Axes preceding-sibling and following-sibling
Next page: Self axis