Big Data Scientific and Commercial Applications Lecture 1: Introduction

Big Data Scientific and Commercial Applications Lecture 1: Introduction

3/22/2016 MANAGING INFORMATION (CSCU9T4) LECTURE 5: XML STYLE Gabriela Ochoa http://www.cs.stir.ac.uk/~goc/ CONTENT Revise XML Schema example Content vs. Display (e.g. Word vs. XML) Ochoa,Gabriela [email protected] XSL: eXtensible Stylesheet Language. A style sheet language for XML documents. XSLT: XSL Transformations. A language for transforming XML documents XPath: a language for navigating in XML documents A number of examples 2 1 3/22/2016 RESOURCES Books XML in a Nutshell (2004) by Elliotte Gabriela Ochoa,Gabriela [email protected] Rusty Harold, W. Scott Means, O'Reilly XML 1.1 Bible (2004) by Elliotte Rusty Harold, Wiley Internet & World Wide Web How to Program, 5/e by Paul J. Deitel, Harvey M. Deitel, Abbey Deitel Links and websites W3 ORG XML Tutorial: W3Schools XML.COM O’REALLY XSLT Online Transformations 3 W3 Schools: XLST online Editor WHAT IS XSL? XSL stands for eXtensible Stylesheet Language, and is a style sheet language for XML documents Gabriela Ochoa,Gabriela [email protected] An XSL style sheet is, like with CSS, a file that describes how to display an XML document of a given type XSL consists of 2 components: XSLT - a language for transforming XML documents XSL-FO - a language for formatting XML documents Related tools XPath - a language for navigating in XML documents. XSLT uses XPath to find information in an XML doc XQuery - a language for querying XML documents 4 2 3/22/2016 HOW DOES XLST WORK? Gabriela Ochoa,Gabriela [email protected] Styling requires: 1. A source XML document, containing the information that the style sheet will display 2. The style sheet itself which describes how to display a 5 document of a given type. XSLT STYLESHEETS Declarative language Use pattern matching and templates to specify the Ochoa,Gabriela [email protected] transformation Vastly more expressive than a CSS stylesheets May perform arbitrary computations XSLT transformation can be done either on the client (e.g. Explorer or Mozilla), or on the server (e.g. Apache Xalan) The target form is usually another XML document, commonly XHTML or HTML 6 3 3/22/2016 XSLT: AN EXAMPLE TRANSFORMATION 1. Demo: Simple ‘Users’ example in the browser with and w/o stylesheet 2. Example at: http://www.w3schools.com/xml/cd_catalog.xml <?xml version="1.0" encoding="ISO8859-1" ?> <CATALOG> XML file Ochoa,Gabriela [email protected] <CD> • No information <TITLE>Empire Burlesque</TITLE> about display <ARTIST>Bob Dylan</ARTIST> • Display is included <COUNTRY>USA</COUNTRY> in the stylesheet <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> … • Use XSLT to transform XML into HTML, before it is displayed in a browser • The browser process the transformation 7 XSL style sheet is an XML file THE CD CATALOG EXAMPLE itself. So, the file begins with an xml declaration <?xml version="1.0"?> The correct way to declare an <xsl:stylesheet version="1.0" XSL style sheet according to the xmlns:xsl=http://www.w3.org/1999/XSL/Transform> W3C XSLT Recommendation <xsl:template match="/"> <html> Gabriela Ochoa,Gabriela [email protected] <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> We must declare the XSLT <th>Title</th> namespace at the top of the <th>Artist</th> document </tr> <xsl:for-each select=“CATALOG/CD"> <tr> <td><xsl:value-of select=“TITLE"/></td> An XSLT stylesheet <td><xsl:value-of select=“ARTIST"/></td> consists of a number of </tr> template rules: rule = </xsl:for-each> pattern + template </table> </body> </html> </xsl:template> </xsl:stylesheet> HTML tags: 8 http://www.tutorialspoint.com/html/html_tags_reference.htm 4 3/22/2016 XSLT PROCESSING MODEL An XSLT stylesheet consists of a number of template rules: template rule = pattern + template For a given input XML document, the output is Ochoa,Gabriela [email protected] obtained as follows: The source tree is processed by processing the root node A single node is processed by: 1. finding the template rule with the best matching pattern 2. instantiating its template (creates result fragment + continues processing recursively) A node list is processed by processing each node in order and concatenating the results Simple illustrative animation: http://cs.au.dk/~amoeller/XML/transformation/p2.html 9 STRUCTURE OF A STYLESHEET An XSLT stylesheet is itself an XML document <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> … Ochoa,Gabriela [email protected] <xsl:template match="pattern"> a template rule template </xsl:template> … </xsl:stylesheet> • Namespace http://www.w3.org/1999/XSL/Transform is used to recognize the XSLT elements. • The XML document may refer to a stylesheet using the processing instruction: <?xml-stylesheet type="text/xsl" href="foo.xsl"?> 10 5 3/22/2016 XLST USES XPATH XPath is an expression language for addressing parts of an XML document Gabriela Ochoa,Gabriela [email protected] Xpath data model provides a tree representation of XML documents as well as atomic values such as integers, strings, and Booleans. It uses path expressions to select a set of nodes or atomic values in an XML document Path expressions look very much like the expressions you see when you work with a traditional computer file system XPath contains a library of standard functions XPath is a W3C recommendation 11 XPATH DATA MODEL XPath sees an XML document as a tree structure The topmost element of the tree is called the root Ochoa,Gabriela [email protected] element. Each information (XML elements, attributes, text, etc.) is called a node. Nodes that XPath can see Root node Elements and attributes Special nodes like comments, processing instructions, namespace declarations. 12 6 3/22/2016 XLST, XML AND TREES The transformation process: XSLT transforms an XML source-tree into an XML result-tree. XML Data Representation Ochoa,Gabriela [email protected] <dependency> <object>sample1.o</object> <depends>sample1.cpp</depends> <depends>sample1.h</depends> <rule>g++ -c sample1.cpp</rule> </dependency> XML Tree representation dependency object depends depends rule 13 sample1.o sample1.cpp sample1.h g++ -c … TREE TERMINOLOGY Root: node without parent (A) A Tree is an abstract model Siblings: nodes share the same parent of a hierarchical structure. Internal node: node with at least one child (A, B, C, F) Ochoa,Gabriela [email protected] External node (leaf ): node without children (E, I, J, K, G, H, D) A Ancestors of a node: parent, grandparent, grand-grandparent, etc. Descendant of a node: child, grandchild, grand-grandchild, etc. B C D Depth of a node: number of ancestors Height of a tree: maximum depth of any node (3) Degree of a node: the number of its E F G H children Degree of a tree: the maximum number of its node. I J K Subtree: tree consisting of a node and its subtree descendants 14 7 3/22/2016 XPATH SYNTAX Let us consider the following XML document <?xml version="1.0" encoding="UTF-8"?> Ochoa,Gabriela [email protected] <bookstore> <book> <title lang="en">Harry Potter</title> <price>29.99</price> </book> <book> <title lang="en">Learning XML</title> <price>39.95</price> </book> </bookstore> 15 XPATH: SELECTING NODES XPath uses path expressions to select nodes in an XML document The most useful path expressions are listed below: Gabriela Ochoa,Gabriela [email protected] Expression Description Nodename Selects all nodes with the name "nodename" / Selects from the root node // Selects nodes in the document from the current node that match the selection no matter where they are . Selects the current node .. Selects the parent of the current node, @ Selects attributes 16 8 3/22/2016 XPATH: EXAMPLES OF PATH EXPRESSIONS Path Expression Selects bookstore All nodes with the name "bookstore" Ochoa,Gabriela [email protected] /bookstore The root element bookstore bookstore/book All book elements that are children of bookstore //book Selects all book elements no matter where they are in the document bookstore//book All book elements that are descendant of the bookstore element, no matter where they are under the bookstore element //@lang All attributes that are named lang 17 PREDICATES Are used to find a specific node or a node that contains a specific value. Are always embedded in square brackets. Gabriela Ochoa,Gabriela [email protected] Path Expression Selects /bookstore/book[1] The first book element that is the child of the bookstore element. /bookstore/book[last()] The last book element that is the child of the bookstore element /bookstore/book[position()<3] The first two book elements that are children of the bookstore element //title[@lang] All the title elements that have an attribute named "lang" //title[@lang='en'] All the title elements that have a "lang" attribute with a value of "en" /bookstore/book[price>35.00] All the book elements of the bookstore element that have a price element with a value greater 18 than 35.00 9 3/22/2016 SELECTING UNKNOWN NODES XPath wildcards can be used to select unknown XML elements. Wildcard Description * Matches any element node Gabriela Ochoa,Gabriela [email protected] @* Matches any attribute node node() Matches any node of any kind Some Examples: Path Expression Selects /bookstore/* All the child element nodes of the bookstore element //* All elements in the document //title[@*] All title elements which have at least one attribute of any kind 19 XPATH: SELECTING SEVERAL PATHS • By using the | operator in an XPath expression you can select several paths. • Some Examples: Ochoa,Gabriela [email protected] Path Expression Select //book/title | //book/price All the title AND price elements of all book elements //title | //price All the title AND price elements in the document /bookstore/book/title | //price All the title elements of the book element of the bookstore element AND all the price elements in the document 20 10 3/22/2016 XPATH AXES An axis defines a node-set relative to the current node.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    20 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us