23
CITA 330 Section 6 XSLT

CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Embed Size (px)

Citation preview

Page 1: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

CITA 330 Section 6

XSLT

Page 2: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Transforming XML Documents to XHTML Documents

• XSLT is an XML dialect which is declared under namespace "http://www.w3.org/1999/XSL/Transform"

• Its root element is stylesheet or transform• Example XSLT stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="4.0“ /> <xsl:template match="/"> <html> <head> <title>DVD Library Listing</title> </head>

Page 3: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Transforming XML Documents to XHTML Documents

<body> <table border="1"> <tr> <th>Title</th> <th>Format</th> <th>Genre</th> </tr> <xsl:for-each select="/library/dvd"> <xsl:sort select="genre“ /> <tr> <td><xsl:value-of select="title“ /></td> <td><xsl:value-of select="format“ /></td> <td><xsl:value-of select="genre“ /></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>

Page 4: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Transforming XML Documents to XHTML Documents

• The root element stylesheet declares a namespace prefix "xsl" for XSL namespace "http://www.w3.org/1999/XSL/Transform"

• The 2nd line’s xsl:output element specifies that the output file of this transformation should follow the specification of HTML v 4.0

• Each xsl:template element specifies a transformation rule: if the document contains nodes satisfying the XPath expression specified by the xsl:template’s match attribute, then they should be transformed based on the value of this xsl:template element

• Since this particular match attribute has value "/" selecting the root element of the input XML document, the rule applies to the entire XML document

Page 5: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Transforming XML Documents to XHTML Documents

• The XSLT template uses an xsl:for-each element to loop through the dvd elements selected by the xsl:for-each element’s select attribute

• In the loop body, the selected dvd elements are first sorted based on their genre value

• Then the xsl:value-of elements are used to retrieve the values of the elements selected by their select attributes

• To use a web browser to transform the earlier file library.xml with this XSLT file library.xsl into HTML, you can add the following line after the XML declaration:

<?xml-stylesheet type="text/xsl" href="library.xsl" ?>

Page 6: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Transforming XML Documents to XHTML Documents

• The resulting Web browser presentation

Page 7: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

XSLT

•An XSLT style sheet contains instructions for transforming the contents of an XML document into another format

•An XSLT style sheet document is itself an XML document

•An XSLT style sheet document has an extension .xsl

Page 8: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

XSLT •An XSLT style sheet converts a source

document of XML content into a result document by using the XSLT processor

Page 9: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

XSLT •The transformation can be performed by a

server or a client•In a server-side transformation, the server

receives a request from a client, applies the style sheet to the source document, and returns the result document to the client

•In a client-side transformation, a client requests retrieval of both the source document and the style sheet from the server, then performs the transformation, and generates the result document

Page 10: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

XSLT•To create an XSLT style sheet, the general

structure is:

<xsl:stylesheet version="1.0"

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

Content of the style sheet

</xsl:stylesheet>

The <xsl:stylesheet> tag can be substituted for the <xsl:transform> tag

Page 11: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Root Template

•A template is a collection of elements that define how a particular section of the source document should be transformed in the result document

•The root template sets up the initial code for the result document

Page 12: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Template

•To create a template, the syntax is:<xsl:template match="node set">

styles

</xsl:template>– where node set is an XPath expression that references a node set from the source document and styles are the XSLT styles applied to those nodes

Page 13: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

•To create a root template, the syntax is:

<xsl:template match="/">styles

</xsl:template>

Root Template

Page 14: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

•A template contains two types of content: XSLT elements and literal result elements

– XSLT elements are those elements that are part of the XSLT namespace and are used to send commands to the XSLT processor

– A literal result element is text sent to the result document, but not acted upon by the XSLT processor

Template

Page 15: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Output Method

•By default, the XSLT processor will render the result document as an XML file

•To control how the processor formats the source document, you can specify the output method using the <xsl:output> element

Page 16: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

xsl:output Attributes

Page 17: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Transforming a Document

•A browser with a built-in XSLT processor allows you to view the result document

•Alternatively, you can use XML tools (such as XML Spy) to create the result document as a separate file, and then view that file in your browser

•Most XSLT processors provide the capability to create the result document as a separate file

Page 18: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Extracting Element Values

•To insert a node’s value into the result document, the syntax is:

– <xsl:value-of select="expression" />– where expression is an expression that

identifies the node from the source document’s node tree

•If the node contains child elements in addition to text content, the text in those child nodes appears as well

Page 19: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Processing Several Elements

•To process a batch of nodes, the syntax is:

<xsl:for-each select="expression"> styles</xsl:for-each>where expression is an expression that defines the group of nodes to which the XSLT and literal result elements are applied

Page 20: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Working with Templates

• To apply a template in the result document, use the XSLT element <xsl:apply-templates

select="expression" /> where expression indicates the node template to be applied

Page 21: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Sorting Node Sets

•By default, nodes are processed in document order, by their appearance in the document

•To specify a different order, XSLT provides the <xsl:sort> element

•This element can be used with either the <xsl:apply-templates> or the <xsl:for-each> element

Page 22: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

•The <xsl:sort> element contains several attributes to control how the XSLT process sorts the nodes in the source document

– The select attribute determines the criteria under which the context node is sorted

– The data-type attribute indicates the type of data

– The order attribute indicates the direction of the sorting (ascending or descending)

Sorting Node Sets

Page 23: CITA 330 Section 6 XSLT. Transforming XML Documents to XHTML Documents XSLT is an XML dialect which is declared under namespace "

Conditional Nodes

•XSLT supports two kinds of conditional elements:

– <xsl:if> – <xsl:choose>

•To apply a format only if a particular condition is met, use the <xsl:if> element

•To test for multiple conditions and display different outcomes, use the <xsl:choose> element