1 / 33

XSL, XSLT and XPath

XSL, XSLT and XPath. Dr. Awad Khalil Computer Science Department AUC. Content. What XSL and XSLT are How XSLT differs from imperative programming languages, like JavaScript What templates are, and how they are used How to address the innards of an XML document using XPath. What is XSL?.

jafari
Télécharger la présentation

XSL, XSLT and XPath

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. XSL, XSLT and XPath Dr. Awad Khalil Computer Science Department AUC XSLT and XPath, by Dr. Khalil

  2. Content • What XSL and XSLT are • How XSLT differs from imperative programming languages, like JavaScript • What templates are, and how they are used • How to address the innards of an XML document using XPath XSLT and XPath, by Dr. Khalil

  3. What is XSL? • Extensible Style sheet Language, as the name implies, is an XML-based language to create style sheets. • The successor to Extensible Stylesheet Language (XSL), XSL Transformations (XSLT) is an XML-based language that enables you to transform one class of XML document to another. • XSLT offers tremendous flexibility for presenting and exchanging data between disparate devices and business systems. For example, with XSLT style sheets, you can dynamically transform an XML purchase order from one schema to another before sending the order to a supplier. • In addition, with XSLT you can dynamically transform an XML document so it can be rendered on a variety of Internet-enabled devices, including handheld PCs and TV set-top boxes. • XSLT is a powerful tool for e-commerce, as well as any other place where XML might be used. XSLT and XPath, by Dr. Khalil

  4. XSLT Engine • An XSL engine uses style sheets to transform XML documents into other document types, and to format the output. • In these style sheets you define the layout of the output document, and where to get the data from within the input document. • In XML, the input document is called the source tree, and the output document the result tree. • There are actually two complete languages under the XSL umbrella: • A transformation language, which is named XSLT • A language used to describe XML documents for display, XSL Formatting Objects XSLT and XPath, by Dr. Khalil

  5. How XSLT Works? • XSLT transforms XML documents into whatever other format – HTML for display on web sites, a different XML-based structure for other applications (such as storing data in a back-end databases), or even just regular text files. • XSLT relies on finding parts of an XML document that match a series of predefined templates, and then applying transformation and formatting rules to each matched part. • XPath – another language – is used to help finding and querying these matching parts. XSLT and XPath, by Dr. Khalil

  6. What Do You Need? • In order to perform an XSLT transformation, you need at least three things: an XML document to transform, an XSLT style sheet, and an XSLT engine. • The style sheet contains the instructions for the transformation you want to perform. • The engine is the software which will carry out the instructions in the style sheet XSLT and XPath, by Dr. Khalil

  7. XT It’s a Win32 executable program that you can download from: http://jclark.com/xml/xml.html You can run XT from the DOS command prompt, using the general syntax: C:\>XT source stylesheet result MSXML It’s XML parser that ships with IE 5 and also includes an XSLT engine. You can download the latest preview from: http://msdn.microsoft.com/downloads/webtechnology/xml/msxml.asp XSLT Engines XSLT and XPath, by Dr. Khalil

  8. How Do XSLT Style Sheets Work? • XSLT style sheets are built on structures called templates. • A template specifies what to look for in the source tree, and what to put into the result tree. • XSLT is written in XML, meaning that there are special XSLT elements and attributes you use to create your style sheets: <xsl:template match=“first”>First tag found!</xsl:template> • Templates are defined using the XSLT <xsl:template> element. There are two important pieces to this template: the match attribute, and the contents of the template. • The match attribute specifies a pattern in the source tree; this template will be applied for any nodes in the source tree that match that pattern – the element named first, in this case. If this template was included in a style sheet, every time the XSLT engine fount a <first> element in the source tree, it would output the text “Firsttagfound!”. • The contents of a template can be much more complex than simply outputting text. The element <xsl:value-of> takes information from the source tree, and adds it to the result tree, for example: <xsl:template match=“first”><xsl:value-of select=“.’ /></xsl:template> XSLT and XPath, by Dr. Khalil

  9. Associating Style Sheets Using PIs • An XSL style sheet can be associated with an XML document using a style sheet processing instruction: <?xml-stylesheet type=“text/xsl” href=“stylesheet.xsl”?> XSLT and XPath, by Dr. Khalil

  10. Assume there are two companies working together, sharing information over the Internet. CompanyA is a store, which sends purchase orders to Company B, who fulfills those orders. CompanyA needs information on the salesperson who made the order for commissioning purposes, but Company B doesn’t need it, but instead needs to know the part numbers, which Company A’s order doesn’t really care about. Company A <?xml version=“1.0” ?> <order> <salesperson>John Doe</salesperson> <item>Production-Class Widget</item> <quantity>16</quantity> <date> <month>1</month> <day>13</day> <year>2000</year> </date> <customer>Sally Finkelstien</customer> </order> Company B <?xml version=“1.0” ?> <order> <date>2000/1/13</date> <customer>Company A</customer> <item> <part-number>E16-25A</part-number> <description> Production-Class Widget </description> <quantity>16</quantity> </item> </order> A Simple E-Commerce Application XSLT and XPath, by Dr. Khalil

  11. The E-Commerce Application – Different Scenarios • Company A can use the same structure for their XML that Company B uses. The disadvantage is that they now a separate XML document to accompany the first one, for their own additional information. • Company B can use the same structure for their XML that Company A uses. • Both companies can use whatever XML format they wish internally, but transform their data to a common format whenever they need to transmit the information outside XSLT and XPath, by Dr. Khalil

  12. 1- Create Company A’s data (CompanyA.xml) <?xml version=“1.0” ?> <order> <salesperson>John Doe</salesperson> <item>Production-Class Widget</item> <quantity>16</quantity> <date> <month>1</month> <day>13</day> <year>2000</year> </date> <customer>Sally Finkelstien</customer> </order> XSLT and XPath, by Dr. Khalil

  13. <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <order> <date> <xsl:value-of select="/order/date/year"/>/<xsl:value-of select="/order/date/month"/>/<xsl:value-of select="/order/date/day"/> </date> <customer>Company A</customer> <item> <xsl:apply-templates select="/order/item"/> <quantity><xsl:value-of select="/order/quantity"/></quantity> </item> </order> </xsl:template> <xsl:template match="item"> <part-number> <xsl:choose> <xsl:when test=". = 'Production-Class Widget'">E16-25A</xsl:when> <xsl:when test=". = 'Economy-Class Widget'">E16-25B</xsl:when> <!--other part-numbers would go here--> <xsl:otherwise>00</xsl:otherwise> </xsl:choose> </part-number> <description><xsl:value-of select="."/></description> </xsl:template> </xsl:stylesheet> 2- Create the XSLT to contain the instructions for transformation (order.xsl) XSLT and XPath, by Dr. Khalil

  14. 3- Perform the transformation (using XT engine) C:\>XT CompanyA.xml order.xsl CompanyB.xsl • The output of this transformation will be Company B’s data: <?xml version=“1.0” encoding=“utf-8” ?> <order> <date>2000/1/13</date> <customer>Company A</customer> <quantity>16</quantity> <item> <part-number>E16-25A</part-number> <description> Production-Class Widget </description> <quantity>16</quantity> </item> </order> XSLT and XPath, by Dr. Khalil

  15. XPath • XPath is a whole separate specification from W3C which is used for addressing and pointing to sections of an XML document, to allow to get the exact pieces of information we need. • XSL uses XPath extensively. • We use XPathexpressions to address XML documents by specifying a location path. • XPath needs to know a context node; that is, the section of the XML document from XPath should start navigation through the document. XSLT and XPath, by Dr. Khalil

  16. Node? What’s a Node? • XPath uses the term node to refer to any part of a document, whether it be element, attribute, or otherwise. • Node-sets are collections of nodes. Node-sets can contain any type of node. • for example, if you tell XPath to look for any elements with an id attribute, XPath will return a node-set, which will be a collection of all the elements in the source tree that have an id attribute. XSLT and XPath, by Dr. Khalil

  17. Location Paths • The first thing to note about XPath is the concept of the document root, which is not the root element. • Since there can be other things at the beginning or end of an XML document before or after the root element, hence, the document root acts as a virtual root of the document’s hierarchy. • Consider the <name> example which has the following XML: <name> <first>John</first> <middle>Fitzgerald Johanson</middle> <last>Doe></last> </name> XSLT and XPath, by Dr. Khalil

  18. Location Paths (Cont’d) • In XPath, the document root is specified by a single “/” as: <xsl:template match=“/”> for which XSLT applies to the entire document. • If we only wanted to match against the <order> element in the order.xml file, instead of the entire document, we could write the XPath expression “ /order”. • XPath reads expressions from left to right, so in this case it reads “start at the document node, and return the <order> element which is a child of that node. • XPath expressions can be read backward as “select any elements named <order> which are children of the document root”. XSLT and XPath, by Dr. Khalil

  19. Location Paths (Cont’d) • XPath expressions can be relative, meaning that they start at the context node, or absolute, meaning that they start at the document root. • Additional steps in instructions to XPath are separated by additional “/” characters, for example, “/order/item/part-number” which means “select any elements named <part-number> which are children of <item> elements, which are children of <order> elements, which are children of the document root. • XPath also allows using the “@” symbol, for example, “@id” means “select the id attribute of the context node”. • The “//” is called the recursive descent operator which locates nodes based on their names, regardless of their locations in the document, for example, “//customer” selects any <customer> element in the document, and “//@id” selects any id attribute in the document. XSLT and XPath, by Dr. Khalil

  20. More Specific Location Paths • “[ ]” brackets are used as a filter on the node being selected, for example: • “order [customer]”matches any<order>element which is a child of the context node and which has a<customer>child element. • “order [@number]”matches any<order>element which is a child of the context node and which has anumberattribute. • “order[customer=‘Company A’]”matches any<order>element which is a child of the context node, and which has a<customer>child with a value of “Company A”. • “customer [. = ‘Company A”] matches any <customer> element with a value of “Company A”. • “//order [customer/@id=‘216A’] says select any <order> elements which have a child element named <customer>, which in turn has an id attribute with a value of “216A”. XSLT and XPath, by Dr. Khalil

  21. Node Functions – are used for working with nodes in general. They can return information about a node, or return specific types of nodes. name() – used to get the name of a node, for example, the following template will output the name of every element that’s child of a <student> element: <xsl:template match=“student”> <xsl:for-each select=“*”> <p><xsl:value-of select=“name()” /> </p> </xsl:for-each> </xsl:template> node() – returns the node itself. Processing-instruction() – returns processing instructions. For example, the following template returns the contents of any processing instructions in the document: <xsl:template match=“processing-instruction()”> <xsl:value-of select=“.” /> </xsl-template> And this one outputs the contents of any PIs in the document with a PITarget of AppName: <xsl:template match=“processing-instruction(‘AppName’)”> <xsl:value-of select=“.” /> </xsl-template> XPath Functions XSLT and XPath, by Dr. Khalil

  22. comment() – used to return comments. for example, the following template will output the text in any comment: <xsl:template match=“comment()”> <xsl:value-of select=“.” /> </xsl-template> text() – returns the PCDATA content of a node, without the PCDATA of the node’s children, if any. For example, if we consider the following XML: <parent>This is some text. <child>And this is some more text</child> </parent> The following template: <xsl:template match=“parent”> <xsl:value-of select=“.” /> </xsl-template> returns: This is some text. And this some more text But, the following template: <xsl:template match=“parent”> <xsl:value-of select=“text()” /> </xsl-template> returns: This is some text. Node Functions (Cont’d) XSLT and XPath, by Dr. Khalil

  23. position() – is used to get the node’s position in a node-set, in document-order. For example, if we consider the following XML: <nodes> <node>a</node> <node>b</node> <node>c</node> </nodes> The template: <xsl:template match=“/nodes/node”> will match any <node> element. The template: <xsl:template match=“/nodes/node[position()=2]”> will match the second <node> element. or: xsl:template match=“/nodes/node[2]”> last() – returns the position of the last node in a node-set. For example, to create a template that matches against the last <node> element, we would do the following: <xsl:template match=“/nodes/node[position() = last()]”> count() – returns the number of nodes in a node-set. For example, to get the number of <node> elements, we would do the following: <xsl:value-of select=“count(node)” /> Positional Functions XSLT and XPath, by Dr. Khalil

  24. number() – converts PCDATA text to a numeric value. For example, if we have an element like this: <element>256</element> According to XPath, this element contains a string containing the characters “2”, “5”, and “6”. In order to to treat the PCDATA as a numeric value of 256, we would have to use the function number(element). sum() – is used to add together all of the numeric values in a node-set. For example, if we have the following XML: <nodes> <node>1</node> <node>5</node> <node>3</node> </nodes> then: <xsl:value-of select=“sum(/nodes/node)” /> returns the sum of all the <node> elements. Numeric Functions XSLT and XPath, by Dr. Khalil

  25. boolean() – simply evaluates an XPath expression to be true or false, using the following rules: If the value is numeric, it’s considered false if it is 0, or the special NaN value, otherwise considered true. If the value is a string, it is true if its length is longer than 0 characters. If the value is a node-set, it is true it it’s not empty, otherwise it’s false. Any other type of object is converted to a Boolean in a way which is dependent on the type of object. not() – simply takes whatever the result and reverses it. true() – always returns true. false() – always returns false. <xsl:if test=“first[.=‘John’]”> <!-- some HTML here …--> </xsl:if> <xsl:if test=“true()”> <!-- some HTML here …--> </xsl:if> <xsl:if test=“false()”> <!-- some HTML here …--> </xsl:if> Boolean Functions XSLT and XPath, by Dr. Khalil

  26. string() string-length() concat() contains() starts-with() substring() substring-after() substring-before() translate() concat(‘mystring’, ‘ ‘, ‘more string’) contains(“This is s string”, “is a”) contains(“This is s string”, “is A”) starts-with(“This is a string”, “This”) starts-with(“This is a string”, “a string”) substring(“This is the main string”, 13) substring(“This is the main string”, 13, 4) substring-after(‘This is the main string’, ‘a’) substring-before(‘This is the main string’, ‘a’) translate(‘QLSM’, ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’, ‘ZYXWVUTSRQPONMLKJIHGFEDCBA’) translate(‘John’, ‘abcdefghijklmnopqrstuvwxyz’, ‘ZYXWVUTSRQPONMLKJIHGFEDCBA’) translate(‘This is a string’, ‘ ‘, ‘+’) translate(‘+bat+’, ‘abc+ ‘, ‘ABC’) <xsl:valueof select = “translate(. , ‘abcdefghijklmnopqrstuvwxyz’, ‘ZYXWVUTSRQPONMLKJIHGFEDCBA’)” /> String Functions XSLT and XPath, by Dr. Khalil

  27. In XML, there are numerous directions we can travel, in addition to just moving up and down between parents and children. The way that we move in these other directions is through different axes. There are 13 axes defined in XPath, which we use in XPath by specifying the axis name, followed by ::, followed by the node name. From our node, we have access to parents and ancestors, children and descendents, and siblings. If the node is an element, we have also access to attributes that are attached to that node. The last section of the tree, we have access to the namespace. Axis Names XSLT and XPath, by Dr. Khalil

  28. The simplest is the self axis, which refers to the context node. The “.” notation is a shortcut for self::node(). The child axis is the default axis and contains the children of the context node. “order” gives exactly the same meaning as “child::order”. There are also wildcards available for child elements and child attributes: “*” selects any child elements, and “@*” selects any child attributes: <xsl:template match=“*”> <xsl:template match=“@*”> <xsl:for-each select=“*”> The descendant axis specifiesany children, or children of children, etc. of the current node, while descendant-or-self axis contains the descendents of the context node, including itself. “//” is the shortcut for descendant-or-self, relative to the document root. “.//” is the descendant-or-self axis of the context node. So, “.//description” means the same as “descendant-or-self::description”. Axis Names (Cont’d) XSLT and XPath, by Dr. Khalil

  29. The parent axis specifies the parent of the context node (if any), and the ancestor axis specifies the parent of the context node, or the parent’s parent, etc. The ancestor-or-self works the opposite way to the descendent-or-self. The following-sibling axis contains all of the following sibling elements of the context node, including their descendents (but not including descendents of the context node). The preceding-sibling axis contains all of the preceding elements of the node, including their descendents (but not including the ancestors of the context node). The attribute axis “@” is used to get at the attributes of the context node. The namespace axis selects the namespace nodes of the context node, if any. Axis Names (Cont’d) XSLT and XPath, by Dr. Khalil

  30. order.xml <?xml version=“1.0” ?> <order number=“312597”> <date>2000/1/1</date> <customer id=“216A”>Company A</customer> <item> <part-number warehouse=“warehouse 11”> E16-25A</part-number> <description> Production-Class Widget </description> <quantity>16</quantity> </item> </order> Xpath.xsl <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform"> <xsl:output method="text“ /> <xsl:template match="/"> <xsl:for-each select="//description/following-sibling::quantity"> <xsl:value-of select="name()“ /> <xsl:text> </xsl:text> </xsl:for-each> </xsl:template> </xsl:stylesheet> An Example: XSLT and XPath, by Dr. Khalil

  31. Other XPath Expressions • child::orderorder selects any <order> elements which are children of the context node. order/* selects any elements which are child of <order>. order/@* selects any attributes which belong to <order>. • descendent-or- .//description selects any <description> elements self:description which are a descendent of the context node, including the context node if applicable. • order/attribute::numberorder/@number selects the number attribute of the <order> node. • //date/parent::order//date/../order selects any <order> elements which are a parent of a <date> element. • /order/descendent::description selects any <description> elements which are descendents of the <order> element • //description/following-sibling::quantity selects a <quantity> element, if that element is a sibling of a <description> node, and it comes after the <description> node in the document order. XSLT and XPath, by Dr. Khalil

  32. Other XPath Expressions (Cont’d) • //quantity/preceding-sibling::description selects a <description> element, if that element is a sibling of the <quantity> node, and it comes before the <quantity> node in document order. • /order/date/following::* selects any elements which come after the <date> element in the document, not including descendents. • /order/item/preceding::* selects any elements which come before the <item> element, not including ancestors. XSLT and XPath, by Dr. Khalil

  33. Thank you XSLT and XPath, by Dr. Khalil

More Related