2.2. Embedding Stylesheets in XML Documents

XSLT stylesheets are often used in a batch environment as separate files, but it is also possible to embed a stylesheet directly in an XML document, possibly to deliver the document directly to a browser for rendering. This means that the XML document must be aware of the stylesheet.

There is a separate recommendation from the W3C that identifies the process for embedding stylesheets in XML documents, called Associating Style Sheets with XML Documents, Version 1.0.[1] This recommendation describes a predefined XML processing instruction, or PI, that can be used at the top of any XML document to allow the XML to find the top of the XSLT stylesheet, wherever it is in the XML.

[1] See http://www.w3.org/TR/xml-stylesheet.

The PI uses an href attribute with a URI to point to the ID of the <xsl:stylesheet> element. This URI must be a fragment identifier (signalled by the # prefix) because it is a reference to a part of the document containing the PI. It is not used to reference an external stylesheet. The following PI model definition shows the two required attributes and four optional attributes for the <?xml-stylesheet?> PI:

<?xml-stylesheet
  href = string
					type = string
  title = string
  media = string
  charset = string
  alternate = "yes" | "no"
?>

Example 2-4, taken directly from the XSLT specification, shows an embedded stylesheet in an XML document.

Notice that this example contains a few elements from the XSL formatting objects specification. The stylesheet is called into play with the <?xml-stylesheet?> PI. The template rule matching on id('foo') in the example finds an element in the XML with an ID of “foo” and uses the instructions in the template to format it, in this case with a bold font.

Example 2-4. Embedding a stylesheet in an XML document.
<?xml-stylesheet type="text/xml" href="#style1"?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>
<head>
<xsl:stylesheet id="style1"
                version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:import href="doc.xsl"/>
<xsl:template match="id('foo')">
  <fo:block font-weight="bold"><xsl:apply-templates/></fo:block>
</xsl:template>
<xsl:template match="xsl:stylesheet">
  <!-- ignore -->
</xsl:template>
</xsl:stylesheet>
</head>
<body>
<para id="foo">
...
</para>
</body>
</doc>

The template rule matching on "xsl:stylesheet" is required in all embedded stylesheets so that <xsl:stylesheet> elements are processed as elements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.22.136