Creating XSLT Stylesheets

XSLT transformations accept a document tree as input and produce a tree as output. From the XSLT point of view, documents are trees built of nodes. XSLT recognizes seven types of nodes XSLT; here are those nodes, along with how XSLT processors treat them:

  • The document root This is the very start of the document.

  • Attribute This node holds the value of an attribute after entity references have been expanded and surrounding whitespace has been trimmed .

  • Comment This node holds the text of a comment, not including <!-- and --> .

  • Element This node consists of all character data in the element, including character data in any of the children of the element.

  • Namespace This node holds the namespace's URI.

  • Processing instruction This node holds the text of the processing instruction, which does not include <? and ?> .

  • Text This node holds the text of the node.

To indicate what node or nodes you want to work on, XSLT supports various ways of matching or selecting nodes. For example, the character / stands for the root node. To get us started, I'll create a short example here that will replace the root nodeand, therefore, the whole documentwith an HTML page.

As you might expect, XSLT stylesheets must be well- formed XML documents, so you start a stylesheet with the XML declaration. Next, you use a <stylesheet> element; XSLT stylesheets use the namespace xsl, which, now that XSLT has been standardized, corresponds to . You must also include the version attribute in the <stylesheet> element, setting that attribute to the only current version, 1.0:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0" xmlns:xsl="">     .     .     . 

That's how you start an XSLT stylesheet (in fact, if you're using a standalone program that requires you to give the name of the stylesheet you're using, you can usually omit the <xsl:stylesheet> element). To work with specific nodes in an XML document, XSLT uses templates. When you match or select nodes, a template tells the XSLT processor how to transform the node for output. In this example, I want to replace the root node with a whole new HTML document. I start by creating a template with the <xsl:template> element, setting the match attribute to the node to match, "/" :

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0" xmlns:xsl="">  <xsl:template match="/">   .   .   .   </xsl:template>  </xsl:stylesheet> 

When the root node is matched, the template is applied to that node. In this case, I want to replace the root node with an HTML document, so I just include that HTML document directly as the content of the <xsl:template> element:

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0" xmlns:xsl="">     <xsl:template match="/">  <HTML>   <HEAD>   <TITLE>   A trivial transformation   </TITLE>   </HEAD>   <BODY>   This transformation has replaced   the entire document.   </BODY>   </HTML>  </xsl:template> </xsl:stylesheet> 

And that's all it takes. By using the <xsl:template> element, I've set up a rule in the stylesheet. When the XSL processor reads the document, the first node it sees is the root node. This rule matches that root node, so the XSL processor replaces it with the HTML document, producing this result:

 <HTML>      <HEAD>         <TITLE>             A trivial transformation         </TITLE>     </HEAD>     <BODY>         This transformation has replaced         the entire document.     </BODY> </HTML> 

That's our first rudimentary transformation. All we've done is replace the entire document with another one. But, of course, that's just the beginning.

Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: