WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 6 (2/5) | WebReference

WebReference.com - Excerpt from Inside XSLT, Chapter 2, Part 6 (2/5)

To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Inside XSLT

Embedded Stylesheets

The XSLT recommendation also supports embedded stylesheets (following the use of embedded stylesheets and style elements in HTML), but like simplified stylesheets, they're not in widespread use.

Not all XSLT processors can handle embedded stylesheets, but some, such as Saxon, do. Here's an example. In this case, I'll include the entire stylesheet element from planets.xsl in planets.xml to create a new document, embedded.xml. This new document will have all the data and the entire stylesheet in it. Note that to be well-formed XML, embedded.xml must have only one root element, so I'll make the stylesheet (that is, the <xsl:stylesheet> element) a child element of the <PLANETS> root element.

To indicate which element is to be treated as the embedded stylesheet, I'll give the <xsl:stylesheet> element the ID "stylesheet" by setting an attribute named id to that name:

<xsl:stylesheet version="1.0" id="stylesheet"
mlns:xsl="http://www.w3.org/1999/XSL/Transform">

I also assign that name, "stylesheet", to the href attribute of the <?xml-stylesheet?> element at the beginning of the document:

<?xml-stylesheet type="text/xml" href="#stylesheet"?>

Now the XSLT processor knows which element I want to use as the stylesheet-the element with the ID "stylesheet". However, which element is that? XML elements are made ID-type elements in XML DTDs or schemas, and as you recall, DTD and schema information is not as yet passed on to the XSLT processor.

Some XSLT processors, such as Saxon, read a DTD, if present, to determine which attributes are of type ID, so I can include a DTD in embedded.xml:

Listing 2.8. planets.xml with an Embedded Stylesheet

<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="#stylesheet"?>
<!DOCTYPE PLANETS [                           
<!ELEMENT PLANET (CUSTOMER)*>                
<!ELEMENT CUSTOMER (NAME,MASS,RADIUS,DAY)>         
<!ELEMENT NAME (#PCDATA)>                  
<!ELEMENT MASS (#PCDATA)>                 
<!ELEMENT RADIUS (#PCDATA)>                      
<!ELEMENT DAY (#PCDATA)>                      
<!ELEMENT xsl:stylesheet (xsl:template)*>                     
<!ELEMENT xsl:template (#PCDATA)>                     
<!ATTLIST xsl:stylesheet
    id ID #REQUIRED
    version CDATA #IMPLIED>
]>
<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>
  <PLANET>
    <NAME>Venus</NAME>
    <MASS UNITS="(Earth = 1)">.815</MASS>
    <DAY UNITS="days">116.75</DAY>
    <RADIUS UNITS="miles">3716</RADIUS>
    <DENSITY UNITS="(Earth = 1)">.943</DENSITY>
    <DISTANCE UNITS="million miles">66.8</DISTANCE><!-At perihelion->
  </PLANET>
  <PLANET>
    <NAME>Earth</NAME>
    <MASS UNITS="(Earth = 1)">1</MASS>
    <DAY UNITS="days">1</DAY>
    <RADIUS UNITS="miles">2107</RADIUS>
    <DENSITY UNITS="(Earth = 1)">1</DENSITY>
    <DISTANCE UNITS="million miles">128.4</DISTANCE><!--At perihelion-->
  </PLANET>
<xsl:stylesheet version="1.0" id="stylesheet"
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">
          <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:template match="xsl:stylesheet"></xsl:template>
</xsl:stylesheet>
</PLANETS>

You should note one final thing: Now that I've included the entire stylesheet in embedded.xml in the <xsl:stylesheet> element, I have to supply a stylesheet template for the <xsl:stylesheet> element. (If I didn't, the text in the stylesheet's text nodes would be copied to the result document as discussed in Chapter 3 in the section on default rules for templates). I leave that element empty by placing the following line at the end of the stylesheet in embedded.xml so nothing is copied from the stylesheet itself to the result document:

<xsl:template match="xsl:stylesheet"></xsl:template>

Now, in Saxon, I can use embedded.xml to create planets.html. In Windows, you use -a to indicate to Saxon that you're using an embedded stylesheet:

C:\planets>saxon -a embedded.xml > planets.html

To page 1current pageTo page 3To page 4To page 5
[previous] [next]

Created: October 16, 2001
Revised: October 16, 2001


URL: http://webreference.com/authoring/languages/xml/insidexslt/chap2/6/2.html