Template based Transformations: XSLT

Software for performing an XSLT transformation, such as JAXP, is known as an XSLT processor.

XSLT document

Input XML Output XML XSLT document document Processor

Processing of XSLT document

XSLT processor is given two inputs—an XSLT document and a source XML document—and produces a single result XML document.

Internally, an XSLT processor represents each of these documents as an XPath tree, called the source tree, the style-sheet tree, and the result tree, respectively.

XSLT processor deals with three XPath trees:

• Input trees: source and style-sheet o Elements containing only white space are normally not included in either input tree (exception::text element) o White space retained within other elements • Output tree: result

Since all three trees are XPath trees, nodes in any of these trees (or more generally, lists of nodes) can be referenced using XPath expressions.

XSLT processors remove all ignorable white space from the trees

XSLT processing (high level):

• Construct input trees • Initialize empty result tree • Search source tree for a node that is matched by a template rule, i.e., a node that is contained in the node list represented by the pattern of some template rule • Instantiate the template of the matching template rule in the result tree o Context node for XPath expressions is matched node

Figure: Illustration of construction of result tree from template instantiation.

• Source tree node was matched by a template rule, producing result tree structure indicated by triangle. • Text node of p element in result tree was produced by instantiating value-of XSLT element, which selected the circled source node and converted it to text. • Text nodes are represented by #text.

Restrictions on XPath in template rule pattern (value of match attribute): • Only child and attribute axes are allowed directly (can indirectly use descendant-or-self axis via // notation) • XPath expression must evaluate to a node list (some XPath expressions are functions that produce string values)

Template markup

in the result tree becomes

  • Most browsers will not accept this notation!

    XSLT does not recognize  

    Solution:

    Adding XML special characters to the result

    • Template:

    Becomes & on input

    • Result: disable-output-escaping also applies to value-of

    • Output formatting: Add :space=“preserve”to transform element of template to retain white space

    Use xsl:output element:

    Displaying XML Documents in Browsers

    Web browsers also offer various levels of support for XML processing.

    First, there is a standard mechanism for associating style information (either CSS or XSLT) with XML documents via an XML processing instruction.

    A processing instruction is a special XML tag that has a syntax similar to that of an XML declaration: the tag begins with .

    Figure: XML document containing an xml-stylesheet processing instruction that associates theX ML document with an XSLT document.

    An XML document can contain a processing instruction telling a browser to apply CSS to style the XML document. For example consider the below XML document.