500 likes | 715 Views
Displaying XML Document. Web and Database Management System. XML and CSS. Web and Database Management System. Using CSS to Display XML Document. This is a simplest way to display the XML document We can simply create a css file with each XML’s tagname representing the separator
E N D
Displaying XML Document Web and Database Management System
XML and CSS Web and Database Management System
Using CSS to Display XML Document • This is a simplest way to display the XML document • We can simply create a css file with each XML’s tagname representing the separator • We can use a browser to open the XML file • The browser will then render the XML document according to the style specified in the CSS file
cd_catalog.css /* CSS Document */ CATALOG { background-color: #ffffff; width:100%; } CD { display:block; margin-bottom:30pt; margin-left:0; } TITLE { color:#FF0000; font-size:20pt; } ARTIST { color:#0000FF; font-size:20pt; } COUNTRY,PRICE,YEAR,COMPANY { display:block; color:#000000; margin-left:20pt; } cd_catalog.xml <?xml-stylesheet type="text/css" href="cd_catalog.css"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> ….. ….. ….. </CATALOG> Display XML Doc using CSS
Result of XML and CSS • To display the xml document with its associated css is to simply open the xml file in a browser
XSLT Web and Database Management System
XSL and XSLT • XSL (EXtensible Stylesheet Language) is a style sheet language for XML documents • XSL describes how an XML document should be displayed • XSL consists of three parts: • XSLT - a language for transforming XML documents • XPath - a language for navigating in XML documents • XSL-FO - a language for formatting XML documents • XSLT stands for XSL Transformations which is how to transform XML documents into other formats
What is XSLT? • XSLT stands for XSL Transformations • XSLT is the most important part of XSL • XSLT transforms an XML document into another XML document • XSLT uses XPath to navigate in XML documents • XSLT is a W3C Recommendation XSLT transforms an XML source-tree into an XML result-tree.
XSLT • XSLT transforms each XML element into an (X)HTML element that can be recognized by browsers • With XSLT we can add/remove elements and attributes to or from the output file • We can rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more • XSLT uses XPath to find information in an XML document
Style Sheet Declaration • The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> or <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
XSL Style Sheet with a Transformation Template <?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> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Link XSL Style Sheet to XML Document <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <year>1985</year> </cd> . . </catalog>
XSLT <xsl:template> Element • A template contains rules to apply when a specified node is matched • The <xsl:template> element is used to build templates • The value of the match attribute is an XPath expression • i.e. match="/" defines the whole document <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version=“1.0” mlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> ………………… </xsl:template> The match="/" attribute associates template with the root of the XML source document.
The <xsl:value-of> Element • <xsl:value-of> is used to extract value of an XML element and add it to the output stream • select attribute in the example, contains an XPath expression <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select=“catalog/cd/title” /></td> <td><xsl:value-of select=“catalog/cd/artist” /></td> </tr> </table>
<xsl:value-of> Output My CD Collection
The <xsl:for-each> Element • <xsl:for-each> element allows us to do looping in XSLT • It can be used to select every XML element of a specified node-set: <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table>
Filtering the Output (for-each) • We can also filter output from XML file by adding a criterion to select attribute in the <xsl:for-each> element • Legal filter operators are: • = (equal) • != (not equal) • < less than • > greater than <xsl:for-each select="catalog/cd[artist='Bob Dylan']">
XSLT <xsl:sort> Element • To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file: <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>
<xsl:sort> Output My CD Collection
Syntax: <xsl:if test="expression"> ...some output if the expression is true... </xsl:if> XSLT <xsl:if> Element • <xsl:if> element is used to put a conditional test against the content of the XML file. <xsl:for-each select="catalog/cd"> <xsl:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each>
<xsl:if> Output My CD Collection
Syntax: <xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose> XSLT <xsl:choose> Element • <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests.
<xsl:choose> Example <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each>
<xsl:choose> Example <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price > 10"> <td bgcolor="#ff00ff"><xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price > 9"> <td bgcolor="#cccccc"><xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each>
XSLT <xsl:apply-templates> Element • <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes • If we add a select attribute to the, it will process only the child element that matches the value of the attribute • We can use the select attribute to specify the order in which the child nodes are processed.
XSLT <xsl:apply-templates> Example <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template>
XPath Web and Database Management System
XPath • XPath is a syntax for defining parts of an XML document • XPath is used to navigate through elements and attributes in an XML document. • XPath is a major element in XSLT
XPath Introduction • XPath Path Expressions • XPath uses path expressions to select nodes or node-sets in an XML document • XPath Standard Functions • XPath has functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more • XPath is Used in XSLT • XPath is a major element in the XSLT standard. Without XPath knowledge we will not be able to create XSLT documents
XPath Nodes • There are seven kinds of nodes: • element, attribute, text, namespace, processing-instruction, comment, and document nodes. <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> <bookstore> is root element node, <author>J K. Rowling</author> is element node, and lang="en" is attribute node.
Terminology • Parent • Each element and attribute has one parent • Children • Element nodes may have zero, one or more children • Siblings • Nodes that have the same parent • Ancestors • A node's parent, parent's parent, etc. • Descendents • A node's children, children's children, etc.
XPath Syntax • XPath uses path expressions to select nodes or node-sets in an XML document. <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>
Selecting Nodes • The most useful path expressions are listed below:
Predicates • Predicates are used to find a specific node or a node that contains a specific value. • Predicates are always embedded in square brackets.
Selecting Unknown Nodes • XPath wildcards can be used to select unknown elements • Selecting nodes using wildcards
Selecting Several Path • By using the | operator in an XPath expression, we can select several paths
Location Path Expression (Axes) • An axis (defines the tree-relationship between the selected nodes and the current node) • a node-test (identifies a node within an axis) • zero or more predicates (to further refine the selected node-set) axisname::nodetest[predicate] <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book> <title lang="eng">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="eng">Learning XML</title> <price>39.95</price> </book> </bookstore>
XPath Functions • XPath provides a group of functions including: • Accessor, Error and Trace, Numeric, String, AnyURI, Boolean, Duration/Date/Time, QName, Node, Sequence, and Context • The default prefix for the function namespace is fn: • The URI of the function namespace is: http://www.w3.org/2005/xpath-functions
XPath Functions Example • Some examples from context function
<html> • <body> • <script type="text/javascript"> • if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari • xmlhttp=new XMLHttpRequest(); • } • else { // code for IE6, IE5 • xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); • } • xmlhttp.open("GET","cd_catalog.xml",false); • xmlhttp.send(); • xmlDoc=xmlhttp.responseXML; • document.write("<table border='1'>"); • var x=xmlDoc.getElementsByTagName("CD"); • for(i=0;i<x.length;i++) { • document.write("<tr><td>"); • document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); • document.write("</td><td>"); • document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); • document.write("</td></tr>"); • } • document.write("</table>"); • </script> • </body> • </html>