Accessing Node Values

Accessing Node Values

You can access the value of a node with the <xsl:value-of> element. This element has two possible attributes:

  • select (mandatory). The value that will be output. Set to an expression.

  • disable-output-escaping (optional). Indicates that characters such as > should be sent to the output as is, without being changed to &gt. Set to yes or no.

The <xsl:value-of> element is always empty.

You can use the select attribute to indicate which node you want to get the value. For example, you might want to get the value of the < NAME > node in each <PLANET> element, which is the text enclosed in that node. You can do that like this:

Listing 2.4 Using <xsl:value-of>
 <?xml version="1.0">  <xsl:stylesheet version="1.0" xmlns::xsl="">      <xsl:template match="/">          <HTML>              <xsl:apply-templates/>          </HTML>      </xsl:template>      <xsl:template match="PLANETS">          <xsl:apply-templates/>      </xsl:template>      <xsl:template match="PLANET">          <P>              <xsl:value-of select="NAME"/>          </P>      </xsl:template>  </xsl:stylesheet> 

The value of a node that contains text is just that text, so here is the result of applying this stylesheet to planets.xml:

 <HTML>      <P>Mercury</P>      <P>Venus</P>      <P>Earth</P>  </HTML> 

Disable-Output-Escaping Attribute

Chapter 3 goes into more detail on the disable-output-escaping attribute of the <xsl:value-of> element.

Suppose that you want to do something a little more advanced, such as transforming the data in planets.xml into an HTML table in the new file planets.html, as you saw in Chapter 1, and which you can see in Figure 2.1. You can do that now with <xsl:value-of> .

Figure 2.1. Planets.html in the Internet Explorer.

Its important to consider one issue here. There is no formal restriction on the order of the <MASS> , <RADIUS> , <DAY> and <DISTANCE> elements in planets.xml, but its important that these elements be processed in a particular order to match the headings of the table. For that reason, I will use the <xsl:value-of> elements in the order that the HTML table needs them.

To create the HTML table you see in Figure 2.1, then, Ill first match the <PLANETS> element, and then replace it with the HTML needed to create the HTML table itself. The <PLANETS> element is a child element of the root node, and because you can refer to the root node as /, you can refer to the <PLANETS> element directly as "/PLANETS" , without having to first use a template for the root node. This is an example of an XPath expression, and youll see many more of them in Chapter 4.

Heres how I start the HTML table by matching the <PLANETS> element directly as "/PLANETS" note that I use <xsl:apply-templates> to apply templates to any child nodes of <PLANETS >:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE BORDER="2">                  <TR>                      <TD>Name</TD>                      <TD>Mass</TD>                      <TD>Radius</TD>                      <TD>Day</TD>                  </TR>                  <xsl:apply-templates/>              </TABLE>          </BODY>      </HTML>  </xsl:template>      .      .      . 

Each <PLANET> child node has a <NAME> , <MASS> , <RADIUS> , and <DAY> child node, and I want to process them in that order so that they are added to the HTML table to match the tables headings. To specify the order in which they should be processed, Ill put the <xsl:value-of> elements in that order:

Listing 2.5 planets.xsl
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0" xmlns::xsl="">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE BORDER="2">                      <TR>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      </TR>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of select="NAME"/></TD>            <TD><xsl:value-of select="MASS"/></TD>            <TD><xsl:value-of select="RADIUS"/></TD>            <TD><xsl:value-of select="DAY"/></TD>         </TR>     </xsl:template>  </xsl:stylesheet> 

Thats all you need; heres the result:

 <HTML>      <HEAD>          <TITLE>              The Planets Table          </TITLE>      </HEAD>      <BODY>          <H1>              The Planets Table          </H1>          <TABLE BORDER="2">              <TR>                  <TD>Name</TD>                  <TD>Mass</TD>                  <TD>Radius</TD>                  <TD>Day</TD>              </TR>              <TR>                  <TD>Mercury</TD>                  <TD>.0553</TD>                  <TD>1516</TD>                  <TD>58.65</TD>              </TR>              <TR>                  <TD>Venus</TD>                  <TD>.815</TD>                  <TD>3716</TD>                  <TD>116.75</TD>              </TR>              <TR>                  <TD>Earth</TD>                  <TD>1</TD>                  <TD>2107</TD>                 <TD>1</TD>             </TR>          </TABLE>      </BODY>  </HTML> 

This is almost what we want. If you look at Figure 2.2, youll see that this HTML file doesnt list the value of the UNITS attribute that each element (except the <NAME> attribute) has in planets.xml:

Figure 2.2. Planets.html without attributes in the Internet Explorer.
 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS>      <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>          .          .          . 

Chapter 3, which works with templates in more detail, shows how to extract the value of the attributes from XML elements.

In the meantime, before you start working with templates in detail, you need to understand a lot more about stylesheets in general. For example, the XSLT 1.1 working draft included support for the XML Base recommendation, which means it will also appear in XSLT 2.0.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: