DOC PREVIEW
Penn CIT 597 - XSLT and XPath LECTURE

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

XSLWhat is XSL?How does it work?Simple XPathSimple XSLTUsing XSL to create HTMLWhat we need to dobooks.xml, revisedDesired HTMLXSL outlineSelecting titles and authorsAll of books.xmlAll of books.xslHow to use itThe result (in IE)The EndJan 14, 2019XSLXSLT and XPath2What is XSL?XSL stands for Extensible Stylesheet LanguageCSS was designed for styling HTML pages, and can be used to style XML pagesXSL was designed specifically to style XML pages, and is much more sophisticated than CSSXSL consists of three languages:XSLT (XSL Transformations) is a language used to transform XML documents into other kinds of documents (most commonly HTML, so they can be displayed)XPath is a language to select parts of an XML document to transform with XSLTXSL-FO (XSL Formatting Objects) is a replacement for CSSWe probably won’t cover XSL-FO3How does it work?The XML source document is parsed into an XML source treeYou use XPath to define templates that match parts of the source treeYou use XSLT to transform the matched part and put the transformed information into the result treeThe result tree is output as a result documentParts of the source document that are not matched by a template are typically copied unchanged4Simple XPathHere’s a simple XML document: <?xml version="1.0"?><library> <book> <title>XML</title> <author>Gregory Brill</author> </book> <book> <title>Java and XML</title> <author>Brett McLaughlin</author> </book></library >XPath expressions look a lot like paths in a computer file system/ means the document itself (but no specific elements)/library selects the root element/library/book selects every book element//author selects every author element, wherever it occurs5Simple XSLT<xsl:for-each select="//book"> loops through every book element, everywhere in the document<xsl:value-of select="title"/> chooses the content of the title element at the current location<xsl:for-each select="//book"> <xsl:value-of select="title"/></xsl:for-each>chooses the content of the title element for each book in the XML document6Using XSL to create HTMLOur goal is to turn this: <?xml version="1.0"?><library> <book> <title>XML</title> <author>Gregory Brill</author> </book> <book> <title>Java and XML</title> <author>Brett McLaughlin</author> </book></library >Into HTML that displays something like this: Book Titles: • XML • Java and XMLBook Authors: • Gregory Brill • Brett McLaughlinNote that we’ve grouped titles and authors separately7What we need to doWe need to save our XML into a file (let’s call it books.xml)We need to create a file (say, books.xsl) that describes how to select elements from books.xml and embed them into an HTML pageWe do this by intermixing the HTML and the XSL in the books.xsl fileWe need to add a line to our books.xml file to tell it to refer to books.xsl for formatting information8books.xml, revised<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="books.xsl"?><library> <book> <title>XML</title> <author>Gregory Brill</author> </book> <book> <title>Java and XML</title> <author>Brett McLaughlin</author> </book></library >This tells you where to find the XSL file9Desired HTML <html> <head> <title>Book Titles and Authors</title> </head> <body> <h2>Book titles:</h2> <ul> <li>XML</li> <li>Java and XML</li> </ul> <h2>Book authors:</h2> <ul> <li>Gregory Brill</li> <li>Brett McLaughlin</li> </ul> </body></html>Red text is data extracted from the XML documentBlue text is our HTML templateWe don’t necessarily know how much data we will have10XSL outline <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> ... </html> </xsl:template> </xsl:stylesheet>11Selecting titles and authors <h2>Book titles:</h2> <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title"/> </li> </xsl:for-each> </ul><h2>Book authors:</h2> ...same thing, replacing title with authorNotice that XSL can rearrange the data; the HTML result can present information in a different order than the XMLNotice the xsl:for-each loop12All of books.xml<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href ="boo ks.xsl"?><library> <book> <title>XML</title> <author>Gregory Brill</author> </book> <book> <title>Java and XML</title> <author>Brett McLaughlin</author> </book></library >Note: if you do View Source, this is what you will see, not the resultant HTML13All of books.xsl <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/ XSL/Transform"><xsl:template match="/"><html> <head> <title>Book Titles and Authors</title> </head> <body> <h2>Book titles:</h2> <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="title"/> </li> </xsl:for-each> </ul> <h2>Book authors:</h2> <ul> <xsl:for-each select="//book"> <li> <xsl:value-of select="author"/> </li> </xsl:for-each> </ul> </body></html></xsl:template></xsl:stylesheet>14How to use itIn a modern browser, just open the XML fileOlder browsers will ignore the XSL and show you the XML contents as continuous textYou can use a program such as Xalan, MSXML, or Saxon to create the HTML as a fileThis can be done on the server side, so that all the client side browser sees is plain HTMLThe server can create the HTML dynamically from the information currently in XML15The result (in IE)16The


View Full Document

Penn CIT 597 - XSLT and XPath LECTURE

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download XSLT and XPath LECTURE
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view XSLT and XPath LECTURE and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view XSLT and XPath LECTURE 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?