Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSLT and XPath, by Dr. Khalil1 XSL, XSLT and XPath Dr. Awad Khalil Computer Science Department AUC.

Similar presentations


Presentation on theme: "XSLT and XPath, by Dr. Khalil1 XSL, XSLT and XPath Dr. Awad Khalil Computer Science Department AUC."— Presentation transcript:

1 XSLT and XPath, by Dr. Khalil1 XSL, XSLT and XPath Dr. Awad Khalil Computer Science Department AUC

2 XSLT and XPath, by Dr. Khalil2 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

3 XSLT and XPath, by Dr. Khalil3 What is XSL? , as the name implies, is an XML-based language to create  Extensible Style sheet Language, as the name implies, is an XML-based language to create style sheets.  The successor to Extensible Stylesheet Language (XSL), is an XML-based language that enables you to transform one class of XML document to another.  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.

4 XSLT and XPath, by Dr. Khalil4 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, and the output document the.  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

5 XSLT and XPath, by Dr. Khalil5 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.

6 XSLT and XPath, by Dr. Khalil6 What Do You Need?  In order to perform an XSLT transformation, you need at least three things: an to transform, an, and an  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

7 XSLT and XPath, by Dr. Khalil7 XSLT Engines   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/downl oads/webtechnology/xml/msx ml.asp

8 XSLT and XPath, by Dr. Khalil8 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: First tag found! First tag found!  Templates are defined using the XSLT 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 element in the source tree, it would output the text “ First tag found! ”.  The contents of a template can be much more complex than simply outputting text. The element takes information from the source tree, and adds it to the result tree, for example:

9 XSLT and XPath, by Dr. Khalil9 Associating Style Sheets Using PIs  An XSL style sheet can be associated with an XML document using a style sheet processing instruction:

10 XSLT and XPath, by Dr. Khalil10 A Simple E-Commerce Application  Assume there are two companies working together, sharing information over the Internet. Company A is a store, which sends purchase orders to Company B, who fulfills those orders. Company A 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 John Doe Production-Class Widget 16 1 13 2000 Sally Finkelstien Company B 2000/1/13 Company A E16-25A Production-Class Widget 16

11 XSLT and XPath, by Dr. Khalil11 The E-Commerce Application – Different Scenarios  can use the same structure for their XML that uses. The disadvantage is that they now a separate XML document to accompany the first one, for their own additional information.  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.  can use the same structure for their XML that uses.  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

12 XSLT and XPath, by Dr. Khalil12 1- Create Company A’s data (CompanyA.xml) John Doe Production-Class Widget 16 1 13 2000 Sally Finkelstien

13 XSLT and XPath, by Dr. Khalil13 2- Create the XSLT to contain the instructions for transformation (order.xsl) / /<xsl:value-of select="/order/date/month"/>/ select="/order/date/month"/>/ Company A Company A </xsl:template> E16-25A E16-25B 00

14 XSLT and XPath, by Dr. Khalil14 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: 2000/1/13 Company A 16 E16-25A Production-Class Widget 16

15 XSLT and XPath, by Dr. Khalil15 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.  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.  uses extensively.  XSL uses XPath extensively.  We use to address XML documents by specifying a.  We use XPath expressions to address XML documents by specifying a location path.  XPath needs to know a ; that is, the section of the XML document from XPath should start navigation through the document.  XPath needs to know a context node ; that is, the section of the XML document from XPath should start navigation through the document.

16 XSLT and XPath, by Dr. Khalil16 Node? What’s a Node?  XPath uses the term to refer to any part of a document, whether it be element, attribute, or otherwise.  XPath uses the term node to refer to any part of a document, whether it be element, attribute, or otherwise.  are collections of nodes. Node-sets can contain any type of node.  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 attribute, XPath will return a node-set, which will be a collection of all the elements in the source tree that have an attribute.  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.

17 XSLT and XPath, by Dr. Khalil17 Location Paths  The first thing to note about XPath is the concept of the, which is not the  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 acts as a virtual root of the document’s hierarchy.  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 example which has the following XML: John Fitzgerald Johanson Doe>

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

19 XSLT and XPath, by Dr. Khalil19 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, ” which means “select any elements named which are children of elements, which are children of elements, which are children of 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 which are children of elements, which are children of elements, which are children of the document root.  XPath also allows using the symbol, for example, ” means “select the id attribute of the context node”.  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, ” selects any <> element in the document, and selects any id attribute in the document.  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 element in the document, and “//@id” selects any id attribute in the document.//@id

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

21 XSLT and XPath, by Dr. Khalil21 XPath Functions  – are used for working with nodes in general. They can return information about a node, or return specific types of nodes.  Node Functions – are used for working with nodes in general. They can return information about a node, or return specific types of nodes.  – 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 element:  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 element:  – returns the node itself.  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: And this one outputs the contents of any PIs in the document with a PITarget of AppName:

22 XSLT and XPath, by Dr. Khalil22 Node Functions (Cont’d)  – used to return comments. for example, the following template will output the text in any comment:  comment() – used to return comments. for example, the following template will output the text in any comment:  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: This is some text. And this is some more text The following template: returns: This is some text. And this some more text But, the following template: returns: This is some text.

23 XSLT and XPath, by Dr. Khalil23 Positional Functions  – is used to get the node’s position in a node-set, in document-order. For example, if we consider the following XML:  position() – is used to get the node’s position in a node-set, in document-order. For example, if we consider the following XML: a b c The template: will match any element. The template: will match the second element. or: 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 element, we would do the following:  count() – returns the number of nodes in a node-set. For example, to get the number of elements, we would do the following:

24 XSLT and XPath, by Dr. Khalil24 Numeric Functions  – converts PCDATA text to a numeric value. For example, if we have an element like this:  number() – converts PCDATA text to a numeric value. For example, if we have an element like this: 256 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 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: 1 5 3 then: returns the sum of all the elements.

25 XSLT and XPath, by Dr. Khalil25 Boolean Functions  – simply evaluates an XPath expression to be true or false, using the following rules:  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.

26 XSLT and XPath, by Dr. Khalil26 String Functions   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’) 

27 XSLT and XPath, by Dr. Khalil27 Axis Names  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.  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.

28 XSLT and XPath, by Dr. Khalil28 Axis Names (Cont’d)  The simplest is the axis, which refers to the context node. The notation is a shortcut for  The simplest is the self axis, which refers to the context node. The “.” notation is a shortcut for self::node().  The axis is the default axis and contains the children of the context node. gives exactly the same meaning as  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:  There are also wildcards available for child elements and child attributes: “*” selects any child elements, and “@*” selects any child attributes:  Theaxis specifiesany children, or children of children, etc. of the current node, while axis contains the descendents of the context node, including itself. is the shortcut for, relative to the document root. is the axis of the context node. So, means the same as “  The descendant axis specifies any 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”.

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

30 XSLT and XPath, by Dr. Khalil30 An Example: order.xml 2000/1/1 Company A E16-25A Production-Class Widget 16 Xpath.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/ Transform">

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

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

33 XSLT and XPath, by Dr. Khalil33 Thank you


Download ppt "XSLT and XPath, by Dr. Khalil1 XSL, XSLT and XPath Dr. Awad Khalil Computer Science Department AUC."

Similar presentations


Ads by Google