Displaying Messages While Processing XSLT Stylesheets

Displaying Messages While Processing XSLT Stylesheets

Here's another useful XSLT elementyou can use the <xsl:message> element to make the XSLT processor display a message, and, optionally , end processing a stylesheet. The <xsl:message> element has one attribute: terminate , which is optional. You set this attribute to "yes" to terminate processing. The default is "no".

Here's an example. In this case, we'll terminate XSLT processing when the XSLT processor tries to transform a <radius> element in ch05_01.xml , displaying the message "Sorry, planetary radius data is restricted." You can see how this works in ch05_14.xsl in Listing 5.13.

Listing 5.13 Using <xsl:choose> ( ch05_14.xsl )
 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.1"  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:message terminate="yes">   Sorry, planetary radius data is restricted.   </xsl:message terminate="yes">   </xsl:template>  <xsl:template match="day">         <xsl:value-of select="."/>         <xsl:text> </xsl:text>         <xsl:value-of select="@units"/>     </xsl:template> </xsl:stylesheet> 

Here's what you see when you use this stylesheetthe XSLT processor will display this message and quit (note that not all XSLT processors will honor the terminate attribute):

 
 Sorry, planetary radius data is restricted. 


XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon

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