Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Namespaces (4/11) >

Creating attributes in some namespace

Handling of attributes is similar to elements.
XSLT

      <xsl:stylesheet
                  xmlns:nnn="namespace-n"
                  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="/aaa">
                  <ooo  nnn:a="1">
                        <xsl:apply-templates  select="*"/>
                  </ooo>
            </xsl:template>

            <xsl:template  match="bbb">
                  <zzz>
                        <xsl:attribute  name="r:x{position()}"
                                    namespace="namespace-b">222</xsl:attribute>
                  </zzz>
            </xsl:template>

            <xsl:template  match="*">
                  <xsl:element  name="{name()}:fff"
                              namespace="namespace-{position()}">
                        <xsl:attribute  name="{name()}:y"
                                    namespace="namespace-{position()}">
                              <xsl:value-of  select="position()"/>
                        </xsl:attribute>
                  </xsl:element>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb/>
            <ccc/>
            <ddd/>
      </aaa>
Output

      <ooo
                  xmlns:nnn="namespace-n"
                  nnn:a="1">
            <zzz
                        xmlns:r="namespace-b"
                        r:x1="222"/>
            <ccc:fff
                        xmlns:ccc="namespace-2"
                        ccc:y="2"/>
            <ddd:fff
                        xmlns:ddd="namespace-3"
                        ddd:y="3"/>
      </ooo>


Previous chapter: Multiple Outputs
Next chapter: Keys
Previous page: Dynamically generated namespaces
Next page: Matching nodes in non-null namespace