1 / 23

CITA 330 Section 6

CITA 330 Section 6. XSLT. 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:

nuri
Download Presentation

CITA 330 Section 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CITA 330 Section 6 XSLT

  2. 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>

  3. 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>

  4. 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

  5. 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" ?>

  6. Transforming XML Documents to XHTML Documents The resulting Web browser presentation

  7. 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

  8. XSLT • An XSLT style sheet converts a source document of XML content into a result document by using the XSLT processor

  9. 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

  10. 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

  11. 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 Root Template

  12. 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

  13. Root Template • To create a root template, the syntax is: <xsl:template match="/"> styles </xsl:template>

  14. Template • 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

  15. 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

  16. xsl:output Attributes

  17. 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

  18. 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

  19. 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 Processing Several Elements

  20. 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 Working with Templates

  21. 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

  22. Sorting Node Sets • 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)

  23. 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

More Related