codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:



Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 Next Page  


The following is a complete sample chapter from Wrox Press's Beginning XSLT
Buy the complete book here

Part 1 : XSLT Templates

Templates

In the last chapter, you saw how to take a well-formed HTML document and turn it into a stylesheet by adding the XSLT elements <xsl:value-of> and <xsl:for-each> to pick out information from a source XML document and produce an HTML result. The stylesheets that we looked at were simplified stylesheets. Simplified stylesheets are good as a starting point when you're creating a stylesheet, and they can be all you need in some cases. However, to utilize the more sophisticated functionality of XSLT, you need to use full stylesheets.

 

In this chapter, we'll take the simplified stylesheet that we developed during the last chapter and turn it into a full stylesheet. I'll also introduce you to templates as a way of breaking up your code and look in a bit more detail at how XSLT processors construct a result from some source XML. You'll learn:

 

        What full stylesheets look like

        How the XSLT processor navigates the source document to create a result

        How to break up your code into separate templates

        How templates help with document-oriented and unpredictable XML

How to create tables of contents in your pages using template modes

         

XSLT Stylesheet Structure

The simplified stylesheets that we used in the last chapter are a specialized form of stylesheet that make a good starting point when we're creating an XSLT stylesheet. Simplified stylesheets aren't all that common in larger applications because they're fairly restricted in what they can do, especially with document-oriented XML.

 

Technically, simplified stylesheets are defined in the XSLT Recommendation in terms of how they map on to full stylesheets. In the last chapter, we developed the following simplified stylesheet (HelloWorld.xsl) to take the Hello World XML document (HelloWorld.xml) and convert it to HTML:


 

<?xml version="1.0" encoding="ISO-8859-1"?>

<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

      xsl:version="1.0">

  <head><title>Hello World Example</title></head>

  <body>

    <p>

      <xsl:value-of select="/greeting" />

    </p>

  </body>

</html>

 

The equivalent full stylesheet for the simplified stylesheet looks very similar. The content of the simplified stylesheet is wrapped in two elements – <xsl:template> and <xsl:stylesheet> – to create HelloWorld2.xsl. The <xsl:stylesheet> element takes the version attribute and the XSLT namespace declaration instead of the <html> element, giving the following:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

  <html>

    <head><title>Hello World Example</title></head>

    <body>

      <p>

        <xsl:value-of select="/greeting" />

      </p>

    </body>

  </html>

</xsl:template>

</xsl:stylesheet>

 

In the next couple of sections, we'll look at what these new XSLT elements do.

Stylesheet Document Elements

The document element of a full stylesheet is <xsl:stylesheet> (a <stylesheet> element in the namespace http://www.w3.org/1999/XSL/Transform – as usual I'm using the prefix xsl here but you could use whatever you liked as long as it's associated with the XSLT namespace with a namespace declaration). Like the document element in simplified stylesheets, the <xsl:stylesheet> element needs to declare the XSLT namespace and give the version of XSLT that's used in the stylesheet with a version attribute. This time, though, the version attribute doesn't need to be qualified with the xsl prefix because it already lives on an element in the XSLT namespace, so the processor knows it's part of XSLT.

 

You can also use <xsl:transform> as the document element in a full stylesheet, rather than <xsl:stylesheet>. There is no difference in functionality between the two document elements – they each use exactly the same attributes and do exactly the same thing. Some people prefer to use <xsl:transform> when doing transformations that aren't producing presentation-oriented formats such as XSL-FO or XHTML. Personally, I use <xsl:stylesheet> all the time.



Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page 10 Page 11 Next Page  




Click here to Buy!

Buy Beginning XSLT here

© Copyright 2002 Wrox Press This chapter is written by Jeni Tennison and taken from "Beginning XSLT" published by Wrox Press Limited in June 2002; ISBN 1861005946; copyright © Wrox Press Limited 2002; all rights reserved.

No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.











Recent Forum Threads
• Perl Unit Test Error
• execute a script in perl script
• Finding data in hash
• Re: finding the mismatches
• Time problem
• vector
• vector
• Math
• Re: Insert Contents of .txt file into a .html page


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008