XML Input A Stylesheet That Generates a Stylesheet That Emulates a for Loop
4.8.2 Template Design
The design of our stylesheet-generating stylesheet is as follows: 1. Output the xsl:stylesheet element. 2. Generate the for-loop template. This will be a named template that well invoke while processing the rest of the document. 3. Generate the root element template. To do this, everything except the for-loop element is copied to the output document. The for-loop element will be converted into a call to the for-loop template we generated in the previous step. 4. Close out the xsl:stylesheet element.4.8.3 Complications
There are a couple of complications in producing our stylesheet-generating stylesheet. First, we need to have some way to distinguish among the XSLT elements in the stylesheet being processed and the XSLT elements were generating. Heres one way to do it: xsl:element name=xsl:template namespace=http:www.w3.org1999XSLTransform xsl:attribute name=namefor-loopxsl:attribute xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameixsl:attribute xsl:attribute name=select xsl:value-of select=index-variable xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameincrementxsl:attribute xsl:attribute name=select xsl:value-of select=increment xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameoperatorxsl:attribute xsl:attribute name=select xsl:textxsl:text xsl:value-of select=operator xsl:textxsl:text xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nametestValuexsl:attribute xsl:attribute name=select xsl:value-of select=test-value xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameiterationxsl:attribute xsl:attribute name=select1xsl:attribute xsl:element ... This lengthy listing generates this simple XML fragment: ns1:template name=for-loop ns1:param name=i select=0 ns1:param name=increment select=1 ns1:param name=operator select== ns1:param name=testValue select=10 ns1:param name=iteration select=1 ... page 76 This approach works, but were doing an awful lot of work to create some fairly simple elements. For all the XSLT elements were generating with xsl:element elements, we have to declare the namespace for each one. The obvious way of handling this would be to generate a namespace declaration on the xsl:stylesheet element: xsl:attribute name=xmlns:xsl http:www.w3.org1999XSLTransform xsl:attribute Unfortunately, the XSLT specification states in section 7.1.3 that this isnt legal. What we did in our previous example was add the namespace attribute to all XSLT elements we need to generate. The XSLT processor is not required to use the namespace prefix we specified in the xsl:element , by the way. To help us get around this awkward problem, the XSLT specification provides the xsl:namespace-alias element. This provision allows us to define an alias for the XSLT namespace or any other namespace we want to use; well use the normal XSLT namespace for the stylesheet elements we use, and well use the alias for the stylesheet elements we generating. Heres how our new stylesheet looks: ?xml version=1.0? xsl:stylesheet version=1.0 xmlns:xsl=http:www.w3.org1999XSLTransform xmlns:xslout=can be anything, doesnt matter xsl:output method=xml indent=yes xsl:namespace-alias stylesheet-prefix=xslout result-prefix=xsl xsl:template match=||text|comment|processing-instruction xsl:copy xsl:apply-templates select=||text|comment|processing-instruction xsl:copy xsl:template xsl:template match=for-loop xslout:call-template name=for-loop xslout:with-param name=i select={index-variable} xslout:with-param name=increment select={increment} xslout:with-param name=operator xsl:attribute name=select xsl:textxsl:text xsl:value-of select=operator xsl:textxsl:text xsl:attribute xslout:with-param xslout:with-param name=testValue select={test-value} xslout:call-template xsl:template xsl:template match=for-loop mode=generate-template xslout:variable name=newline xslout:text xslout:text xslout:variable xslout:template name=for-loop xslout:param name=i select=index-variable xslout:param name=increment select=increment xslout:param name=operator select=operator xslout:param name=testValue select=test-value xslout:param name=iteration select=1 xslout:variable name=testPassed xslout:choose xslout:when test=starts-withoperator, = xslout:if test=i = testValue xslout:texttruexslout:textParts
» O'Reilly-XSLT-Mastering.XML.Transformati... 2264KB Mar 29 2010 05:03:43 AM
» An XML document must be contained in a single element
» XML declarations Document Type Definitions DTDs and XML Schemas
» Well-formed versus valid documents
» Tags versus elements XML Document Rules
» Namespaces XML Document Rules
» The Extensible Stylesheet Language XSL
» Document Object Model DOM Level 1
» Document Object Model DOM Level 2
» Namespaces in XML XML Standards
» Associating stylesheets with XML documents
» Installing Xalan Getting Started
» Our Sample Document A Sample Stylesheet
» Transforming the XML Document
» Stylesheet Results Transforming Hello World
» Parsing the Stylesheet How a Stylesheet Is Processed
» Parsing the Transformee How a Stylesheet Is Processed
» Lather, Rinse, Repeat How a Stylesheet Is Processed
» The xsl:template for greeting Elements
» Built-in template rule for element and root nodes
» Built-in template rule for modes
» Built-in template rule for text and attribute nodes
» Top-Level Elements Stylesheet Structure
» Other Approaches Stylesheet Structure
» The Hello World Java Program
» Goals of This Chapter Summary
» The Root Node The XPath Data Model
» Element Nodes The XPath Data Model
» Attribute Nodes The XPath Data Model
» Text Nodes The XPath Data Model
» Comment Nodes The XPath Data Model
» Processing Instruction Nodes The XPath Data Model
» Namespace Nodes The XPath Data Model
» Simple Location Paths Location Paths
» Relative and Absolute Expressions
» Selecting attributes Selecting Things Besides Elements with Location Paths
» Selecting the text of an element
» Selecting comments, processing instructions, and namespace nodes
» Using Wildcards Location Paths
» Numbers in predicates Predicates
» Functions in predicates Predicates
» Attribute Value Templates XPath Datatypes
» Output View The XPath View of an XML Document
» The Stylesheet The XPath View of an XML Document
» Summary XPath: A Syntax for Describing Needles and Haystacks
» Converting to boolean values
» Boolean examples The xsl:if Element
» xsl:for-each example The xsl:for-each Element
» How It Works Invoking Templates by Name
» Templates à la Mode Invoking Templates by Name
» Defining a Parameter in a Template
» Microsofts XSLT tools Global Parameters
» Setting global parameters in a Java program
» Are These Things Really Variables?
» Procedural design Implementing a String Replace Function
» Recursive design Implementing a String Replace Function
» Template Design Implementation A Stylesheet That Emulates a for Loop
» The Complete Example A Stylesheet That Emulates a for Loop
» XML Input A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Template Design A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Complications A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Summary Branching and Control Elements
» The ID, IDREF, and IDREFs Datatypes
» An XML Document in Need of Links
» A Stylesheet That Uses the id Function
» Limitations of IDs Generating Links with the id Function
» Defining a key Generating Links with the key Function
» A Slightly More Complicated XML Document in Need of Links
» The key function and the IDREFS datatype
» Solution 1: Replace the IDREFS datatype
» Solution 2: Use the XPath contains function
» Solution 3: Use recursion to process the IDREFS datatype
» Solution 4: Use an extension function
» Advantages of the key Function
» An Unstructured XML Document in Need of Links
» The generate-id Function Generating Links in Unstructured Documents
» Summary Creating Links and Cross-References
» Our First Example Sorting Data with xsl:sort
» Whats the deal with that syntax?
» Attributes The Details on the xsl:sort Element
» Another Example Sorting Data with xsl:sort
» Our First Attempt Grouping Nodes
» A Brute-Force Approach Grouping with xsl:variable
» Summary Sorting and Grouping Elements
» Recursive design An Aside: Doing Math with Recursion
» Generating output to initialize a variable
» Overview Invoking the document Function
» The document Function and Sorting
» Implementing Lookup Tables More Sophisticated Techniques
» Grouping Across Multiple Documents
» Summary Combining XML Documents
» Example: Generating multiple output files
» Example: Using extension functions from multiple processors
» Example: A library of trigonometric functions
» Example: Writing extensions in other languages
» Fallback Processing Extension Elements, Extension Functions, and Fallback Processing
» Extending the Saxon Processor
» Generating JPEG Files from XML Content
» About the Toot-O-Matic Case Study: The Toot-O-Matic
» Make It Easier to Create Tutorials
» Individual Panels Tutorial Layout
» Email Panel Zip File PDF Files
» Individual Panels XML Document Design
» Stylesheets and Modes XSLT Source Code
» Initializing Global Variables XSLT Source Code
» Generating the Main Menu Panel
» Generating the Section Indexes
» Generating the Individual Panels
Show more