This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
 
Page 12 of 15

 

Previous Page Table Of Contents Next Page

XSL Transformations, cont.

Generating Text with xsl:text

You can create text nodes with the <xsl:text> element, allowing you to do things such as replace whole elements with text on the fly. One reason you can use <xsl:text> is to preserve whitespace, as in this example from earlier in the chapter, where I used <xsl:text> to insert spaces:

<?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>

                    <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>

      </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:value-of select="."/>

      <xsl:text> </xsl:text>

      <xsl:value-of select="@UNITS"/>

    </xsl:template>

 

</xsl:stylesheet>

Another reason to use <xsl:text> is when you want characters such as < and & to appear in your output document, not &lt; and &amp;. To do that, you set the <xsl:text> element's disable-output-escaping attribute to "yes":

<?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>

                Planets

            </TITLE>

        </HEAD>

        <BODY>

            <xsl:apply-templates select="PLANET"/>

        </BODY>

    </HTML>

</xsl:template>

 

<xsl:template match="PLANET">

    <xsl:text disable-output-escaping = "yes">

        &lt;PLANET&gt;

    </xsl:text>

</xsl:template>

 

</xsl:stylesheet>

Here is the result:

<HTML>

<HEAD>

<TITLE>

                Planets

            </TITLE>

</HEAD>

<BODY>

      <PLANET>

 

      <PLANET>

 

      <PLANET>

  </BODY>

</HTML>

 

Page 12 of 15

 

Previous Page Table Of Contents Next Page
 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP