Using the Or Operator

Using the Or Operator

Using the Or operator, , you can match to a number of possible patterns, which is very useful when your documents get a little more involved. In the following example, I want to display < NAME > and <MASS> elements in bold, which I do with the HTML <B> tag. To match either <NAME> or <MASS> elements, Ill use the Or operator in a new rule:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>              .              .              .              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:apply-templates select="NAME"/></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>      <xsl:template match="NAME  MASS">          <B>              <xsl:apply-templates/>          </B>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>  </xsl:stylesheet> 

Here are the results; note that the name and mass values are both enclosed in <B> elements:

 <HTML>      <HEAD>          <TITLE>              The Planets Table          </TITLE>      </HEAD>      <BODY>          .          .          .              <TR>                  <TD><B>Mercury</B></TD>                  <TD><B>.0553</B></TD>                  <TD>1516 miles</TD>                  <TD>58.65 days</TD>              </TR>              <TR>                  <TD><B>Venus</B></TD>                  <TD><B>.815</B></TD>                  <TD>3716 miles</TD>                  <TD>116.75 days</TD>              </TR>              <TR>                  <TD><B>Earth</B></TD>                  <TD><B>1</B></TD>                  <TD>2107 miles</TD>                  <TD>1 days</TD>              </TR>          </TABLE>      </BODY>  </HTML> 

You can use any valid pattern with the operator, such as expressions like "PLANET PLANET//NAME" , and you can use multiple operators, like "NAME MASS DAY" , and so on.



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

Similar book on Amazon

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