Advanced XSLT II
Iteration in XSLT we sometimes wish to apply the same transform to a set of nodes we iterate through a node set the node set is defined by an XPath expression we use a for-each element to control the iteration
Applies processing to all nodes in a given node set select attribute contains XPath expression defining the node set the current node changes at each iteration of for-each –changes the context for transforms of content
example we have XML data for a tutorial, divided into sections we wish to produce a numbered contents list in HTML we use for-each twice to create a numbered heading and a list of sections we use the position() function for the numbering
XML Tutorial XML and DTDs XPath and XSLT The Data
Section 1. XML Tutorial 1. XML and DTDs 2. XPath and XSLT … and so on for each section The Desired Output
Section.. </xsl:template
example - notes for-each select=section selects all section nodes in the current tutorial –position() will number the sections in document order for-each select=panel selects all panel nodes in the current section this is just a nested loop!
modes Sometimes you may wish to transform the same node in more than one way The transform for a node is defined in a template A mode attribute is available for template and apply-templates elements This allows more than one template to be defined for any node
example XML data for a famous scientists website The data contains a name element for each person featured on the site. We use mode to achieve two different presentations of name –table of contents –personal data entry
Alan Turing computer scientist mathematician cryptographer The Data
Richard P Feynman physicist playing the bongoes The Data
The Desired Output Turing, Alan Feynman, Richard Name: Alan Turing Born: 1912 Died: 1954 Professions: computer scientist, mathematician, cryptographer Hobbies: none known
<xsl:stylesheet version = 1.0 xmlns:xsl= > Famous Scientists <xsl:apply-templates select = person mode = toc />
,
Name: Born: Died:
attribute value templates allows attribute values derived from the source to be set in the output example <name first = {first_name} initial = {middle_initial} last = {last_name} />
specifies a particular template to be invoked perhaps not related directly to a node-set –e.g. to add boilerplate start_html markup template must have a name attribute calls the template by name
<!Interesting stuff that generates the masthead goes in here --> … some-title
, defines parameters for templates parameters can be passed to templates from either call-template or apply-template elements containing elements name attribute names the parameter select attribute (or content) defines the value of the parameter
XSLT processing flow reference to template and parameter passed return to XSLT processing flow
150 …
allows values to be stored to assist with processing flow logic name attribute names the variable select attribute (or content) defines the value of the variable value can also be defined from element content
, $y = 13 else $y = 15 -->
appears inside or inside sorts a node-set according to some criterion successive sort elements impose sub-orders select attribute defines the sorting criterion data-type defines the type of sort –text (the default) –number
more sort attributes order –ascending (default) –descending case-order –upper-first –lower-first
document ( ) converts a parameter into a URI –relative to current document –second parameter gives an alternative base URI returns a node-set associated with the URI allows content to be extracted from multiple XML documents without parameter defaults to current document
multiple XML files XSLT document XSLT processor output document set of template rules match elements and replace using template rules XML source XML source XML source XML source XML source XML source master document document()
Fridge Contents XML XSLT
XSLT extensions extension elements are passed to external code (extension functions) for processing –Java, Perl, C# etc. extends power of XSLT to arbitrary transformations implementation varies between XSLT processors –Xalan, Saxon, XT all allow extensions, but in different ways