The xsl:message Element

The <xsl:message> Element

You can use the <xsl:message> element to cause the XSLT processor to display a message, and, optionally , quit processing a stylesheet. The <xsl:message> element has one attribute:

  • terminate (optional). Set to yes to terminate processing. The default is no.

Where the message is actually sent depends on the XSLT processor. For Java-based XSLT processors, the message is usually sent to the Java error output stream, which corresponds to the screen if you invoke the XSLT processor on the command line. Other XSLT processors may display messages in pop-up windows , or in Web pages sent to browsers.

Heres an example. In this case, Ill terminate XSLT processing when the XSLT processor tries to transform a <DAY> element in planets.xml, displaying the message "Sorry, DAY information is classified ." :

Listing 3.12 Using <xsl:message>
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <TABLE BORDER="2">                      <TD>Name</TD>                      <TD>Mass</TD>                      <TD>Radius</TD>                      <TD>Day</TD>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><xsl:value-of 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="MASS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </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:message terminate="yes">              Sorry, DAY information is classified.          </xsl:message terminate="yes">      </xsl:template>  </xsl:stylesheet> 

Here are the results when you use this stylesheet with Xalan:

 C:\planets>java org.apache.xalan.xslt.Process -IN planets.xml -XSL message.xsl -OUT graphics/ccc.gif planets.html  file:///C:/XSL/messages/message.xsl; Line 49; Column 38; Sorry, DAY information is graphics/ccc.gif classified.  XSLT Error (javax.xml.transform.TransformerException): Stylesheet directed termination 

Using <xsl:message> , you can display information about whats going on during stylesheet processing, which is good not only for displaying errors and warnings, but also to debug stylesheets.

More template topics are coming up throughout the book, such as calling named templates and using template parameters. In the next chapter, Ill take a look at a large and important topic: how to create the match patterns you use to specify what node or nodes you want to work with in XSLT. Ive already touched on how to create and use match patterns, but its time to take a systematic look at match patterns.



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