Using the namespace Axis

Using the namespace Axis

The namespace axis contains the namespace nodes of the context node. Note that the axis is empty unless the context node is an element. An element has a namespace node for

  • Every attribute of the element whose name starts with xmlns:.

  • Every attribute of an ancestor element whose name starts xmlns: (unless, of course, the element itself or a nearer ancestor redeclares the namespace).

  • An xmlns attribute, if the element, or some ancestor, has an xmlns attribute.

In the following example, Im going to display the namespace of the <PLANETS> element in the result document, and Im setting that namespace to http://www.starpowder.com in the source document:

Listing 7.10 planets.xml with a Namespace Declaration
 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS xmlns="http://www.starpowder.com">      <PLANET>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>          .          .          . 

Heres the stylesheet, where I check the namespaces used in the <PLANETS> element:

Listing 7.11 Using the namespace axis in planets.xml
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:output method="xml"/>  <xsl:template match="PLANETS">      <xsl:value-of select="namespace::*"/>  </xsl:template>  </xsl:stylesheet> 

And heres the result document (note that this result may vary by XSLT processor):

 <?xml version="1.0" encoding="UTF-8"?>  http://www.starpowder.com 


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net