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

Discovering available namespaces on the element

In XPath 1.0 "namespace::" axis was used for namespace discovery. In XPath 2.0 this axis was deprecated and XPath 2.0 compatible processors are not required to support this axis.

Functions in-scope-prefixes() and namespace-uri-for-prefix() are used for this task.

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="/aaa">
                  <xxx>
                        <xsl:for-each  select="descendant::*">
                              <xsl:variable  name="v"
                                          select="."/>
                              <el  name="{name($v)}">
                                    <xsl:for-each  select="in-scope-prefixes(.)">
                                          <nm  prefix="{.}"
                                                      namespace="{namespace-uri-for-prefix(.,$v)}"/>
                                    </xsl:for-each>
                              </el>
                        </xsl:for-each>
                  </xxx>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <o:bbb
                        xmlns:o="oooooo">
                  <p:ccc
                              xmlns:p="pppppp"/>
            </o:bbb>
            <ccc
                        xmlns:="cccccc"/>
      </aaa>
Output

      <xxx>
            <el  name="o:bbb">
                  <nm  prefix="xml"
                              namespace="http://www.w3.org/XML/1998/namespace"/>
                  <nm  prefix="o"
                              namespace="oooooo"/>
            </el>
            <el  name="p:ccc">
                  <nm  prefix="xml"
                              namespace="http://www.w3.org/XML/1998/namespace"/>
                  <nm  prefix="o"
                              namespace="oooooo"/>
                  <nm  prefix="p"
                              namespace="pppppp"/>
            </el>
            <el  name="ccc">
                  <nm  prefix="xml"
                              namespace="http://www.w3.org/XML/1998/namespace"/>
                  <nm  prefix=""
                              namespace="cccccc"/>
            </el>
      </xxx>


Previous chapter: Multiple Outputs
Next chapter: Keys
Previous page: Namespace creation
Next page: - - -