XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain text, another XML, etc.) - Why do we need to learn it? - A typical work flow: legacy data source > XML > XSL transformation > application/Web browser XPath - a language for navigating in XML documents (XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents) XSL-FO - a language for formatting XML documents
How Does It Work: XSLT stylesheet XML document XSLT processor Transformed XML/HTML document An XSLT processor can be built into a web browser, or can be a standalone program run from the command (e.g. SAXON or Apache Xalan)
Example source XML file: XSLT stylesheet: Link the XSL Style Sheet to the XML Document: Result XML file:
Example source xml file: XSLT stylesheet: Link the XSL Style Sheet to the XML Document: Result XML file: Empire Burlesque Bob Dylan USA Columbia
Example source xml file: XSLT stylesheet: Link the XSL Style Sheet to the XML Document: Result XML file: My CD Collection Title Artist
Example source xml file: XSLT stylesheet: Link the XSL Style Sheet to the XML Document: Result XML file: Empire Burlesque Bob Dylan USA Columbia
Example source XML file: XSLT stylesheet: Link the XSL Style Sheet to the XML Document: Result XML file: My CD Collection Title Artist Empire Burlesque Bob Dylan …
Template Rules templatesAn XSL style sheet consists of one or more set of rules that are called templates. template elementsEach template rule tells the XSLT processor how to handle the specific elements in the input XML document.
Template Rules (Example) root element sub-element “cd” of “catalog” … title: value of element “title”
Structure of a stylesheet …… \ template > a template rule / ……
Processing Model For a given input XML document, the output is obtained as follows: - The source tree is processed by processing the root node - a node list is processed by processing each node in order and concatenating the results - a single node is processed by: finding the template rule with the matching pattern, and creating result fragment
My CD Collection Title Artist Empire Burlesque Bob Dylan USA Columbia … My CD Collection TitleArtist Empire BurlesqueBob Dylan
XSL:for-each <xsl:for-each select = “node-set-expression” XSL:if <xsl:if test = “boolean-expression” XSL:sort <xsl:sort select = “string-expression” data-type = “text” | “number” | “prefixedName” lang = “langcode” order = “ascending” | “descending” case-order = “upper-first” | “lower-first” />
My CD Collection Title Artist My CD Collection TitleArtist Empire BurlesqueBob Dylan Greatest HitsDolly Parton The very best ofCat Stevens
My CD Collection Title Artist My CD Collection TitleArtist Empire BurlesqueBob Dylan filter
My CD Collection Title Artist My CD Collection TitleArtist Empire BurlesqueBob Dylan The very best ofCat Stevens Greatest HitsDolly Parton Sort On an element
My CD Collection Title Artist My CD Collection TitleArtist Empire BurlesqueBob Dylan Greatest HitsDolly Parton if test inside
My CD Collection Title Artist My CD Collection TitleArtist Empire BurlesqueBob Dylan The very best ofCat Stevens Greatest HitsDolly Parton Multiple conditional test
My CD Collection Title Artist … My CD Collection TitleArtist Empire BurlesqueBob Dylan The very best ofCat Stevens Greatest HitsDolly Parton Multiple conditional test 2
XSL:apply-templates <xsl:apply-templates select=“node-set-expression” mode=“qualifiedName”> Without xsl:sort child elements, the default is in document order; xsl:with-param child elements can be used to pass parameter values to the matched templates
My CD Collection Title: Artist: My CD Collection Title: Empire Burlesque Artist: Bob Dylan Title: Hide your heart Artist: Bonnie Tyler Title: Greatest Hits Artist: Dolly Parton
… Born: Died: … Select attribute value in … Born: 1918 Died: 1988 …
<name first=“{first_name}” initial=“{middle_initial}” last=“{last_name}” /> /xsl:template> … Attribute Value Templates Richard P Feynman
Using oXygen XML Editor
Other Issues in Practice XPath - a language for finding information in an XML document. XPath functions Be aware of the “context” Namespace (e.g. “dc:author”)
References: This PPT is based on XSLT Tutorial at: Zvon XSL Tutorial: XSLT reference: