Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming XPath, XSL / XSLT.

Similar presentations


Presentation on theme: "1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming XPath, XSL / XSLT."— Presentation transcript:

1 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming XPath, XSL / XSLT

2 Dickson Chiu-2004CSIT600b 02-2 XPath Introduction The primary purpose of XPath is to address parts of an XML document Resources http://www.w3c.org/TR/xpath http://www.vbxml.com/xsl/XPathRef.asp Used by XSLT and XPointer XSLT to transform XML documents into other formats XPointer for “pointing” to a document’s contents Non-XML syntax to facilitate use with URI’s and attribute values A path notation like that of URL for navigation catalog/cd/title

3 Dickson Chiu-2004CSIT600b 02-3 XPath 1.0 XPath 1.0 is a W3C recommendation (16 November 1999) The recommendation consists of Location paths for getting around Expressions the primary syntax constructs Core function library every implementation must include Data model where an XML document is viewed conceptually as a tree of nodes See Tutorial: http://zvon.org/xxl/XPathTutorial/Output/index.html http://zvon.org/xxl/XPathTutorial/Output/index.html

4 Dickson Chiu-2004CSIT600b 02-4 Tree and Nodes Seven types of nodes root nodes element nodes text nodes attribute nodes namespace nodes processing instruction nodes comment nodes The XPath tree has a single root node

5 Dickson Chiu-2004CSIT600b 02-5 An Example (Fig. 11.2) Root Comment Fig. 11.1 : simple.xml Comment Simple XML document Element book Attribute Title C++ How to Program Attribute edition 3 Element sample Text // C++ comment if (this -> getX() displayError(); Text C++ How to Program by Deitel & Deitel <![CDATA[ // C++ comment if ( this->getX() < 5 && value[ 0 ] != 3 ) cerr displayError(); ]]> C++ How to Program by Deitel & Deitel

6 Dickson Chiu-2004CSIT600b 02-6 XPath Example Java How to Program Spanish Chinese Japanese French Japanese C++ How to Program Korean French Spanish Italian Japanese /books/book/translation[. = ‘Japanese’]/../title /books/book/translation[. = ‘Japanese’]/@edition

7 Dickson Chiu-2004CSIT600b 02-7 String-value & Expanded-name Every node has a string representation – its string value Used in comparison Some string-values are concatenation of all the string-values of descendant nodes Some types of node also have an expanded- name A pair consisting of a local part (a string) and a namespace URI (could be null) Used to locate specific nodes in the tree

8 Dickson Chiu-2004CSIT600b 02-8 Some Node Types (Fig. 11.5)

9 Dickson Chiu-2004CSIT600b 02-9 Node Types (cont’d)

10 Dickson Chiu-2004CSIT600b 02-10 Location Paths An expression to specify how to navigate from one node to another A series of location steps, leading to the target node(s) The starting point is a context node A location step has an axis, a node test, and an optional predicate; for example child::text()[position()=1] child is the axis, text() the node test, and [position()=1] a predicate

11 Dickson Chiu-2004CSIT600b 02-11 XPath Axes

12 Dickson Chiu-2004CSIT600b 02-12 Some Node Tests To refine the set of nodes selected by an axis Select by node name or type child::* child::text() child::*/child::text()

13 Dickson Chiu-2004CSIT600b 02-13 Abbreviations body = child::body //body = /descendant-or-self::node()/child::body

14 Dickson Chiu-2004CSIT600b 02-14 Node-set Operators head|body //book

15 Dickson Chiu-2004CSIT600b 02-15 Node-set Functions head/title[last()] book[position()=3] book[3] count(*)

16 Dickson Chiu-2004CSIT600b 02-16 The Stock XSLT Example <xsl:if test = "starts-with(@symbol, 'C')"> <xsl:value-of select = "concat(@symbol,' - ', name)"/> CSCO - Cisco Systems CMGI - CMGI, Inc. <?xml:stylesheet type = "text/xsl" href = “stock.xsl"?> Intel Corporation Cisco Systems CMGI, Inc.

17 Dickson Chiu-2004CSIT600b 02-17 XSL - Extensible Stylesheet Language Transforms an input document (source tree) to an output document (result tree) By matching XPath expression – data driven (cf. Prolog) Two (language) components XSLT, T for Transformation XSL FO, FO for Formatting Objects XPath was the query language part of the original XSLT proposal XSLT 1.0 is a W3C recommendation, so is XPath 1.0; (now working on 2.0) XSL 1.0 (with FO) recommendation Oct 15, 2001. http://www.w3.org/Style/XSL/ http://www.w3.org/Style/XSL/XSL-FO/ Tutorial: http://www.zvon.org/xxl/XSLTutorial/Output/index.htmlhttp://www.zvon.org/xxl/XSLTutorial/Output/index.html

18 Dickson Chiu-2004CSIT600b 02-18 XSLT for E-Commerce Companies sharing information Company A sends purchase order to Company B A and B need different sets of information related to the purchase Using XSLT, the same (master) data can be transformed to suit the needs of A and B The companies can use XSL FO to further modify the transformed data for display Compare views in databases…

19 Dickson Chiu-2004CSIT600b 02-19 XSLT/XPath Processors IE IE5 need to install the latest MSXML (3.0) in “replace mode” IE6 – with MSXML 3.0 Latest version: MSXML 4.0 SP1…MSXML 4.0 SP1 Apache Xalan Need to work with a Java-based XML parser such as Xerces XT By one of the champions of the W3C XSL effort Has a Win32 executable version Many others

20 Dickson Chiu-2004CSIT600b 02-20 XSLT stylesheets are simply a collection of templates To apply to an input document to create an output document A template specifies The section of the source tree to which the template applies, using the match attribute (see: zvon.org tutorial p6 p8 p9)p6p8p9 The output that will be inserted into the result tree – everything between and Other attributes: name param – specific parameter for call-template (see: zvon.org tutorial p34)zvon.org tutorial p34 priority – ( numeric) order upon multiple template match (see: zvon.org tutorial p10)zvon.org tutorial p10 mode – for advanced programming: context match but mode value does not: skip (see: zvon.org tutorial p11)zvon.org tutorial p11

21 Dickson Chiu-2004CSIT600b 02-21 Used within a template, to call other templates Recursive templates allowed! Uses the select attribute to select a context node If empty, the current node is selected, as in the following default template (see: zvon.org tutorial p7)p7 Note difference from subroutine call – more than 1 match possible (cf. Prolog) Example <xsl:apply-templates select=“/Team/Member”/>

22 Dickson Chiu-2004CSIT600b 02-22 To do something useful with the information in the source tree Searches the context node for the value specified in the select attribute, and inserts it into the result tree

23 Dickson Chiu-2004CSIT600b 02-23 What if we don’t know ahead of time what elements we are creating? The following element creation depends on the contents of a source element Hello produces Hello when run against Tommy in the source tree See: zvon.org tutorial p22zvon.org tutorial p22 You may fill in attributes too. (See: zvon.org tutorial p23)zvon.org tutorial p23

24 Dickson Chiu-2004CSIT600b 02-24 and evaluates the expression in the test attribute; if true, the contents of is evaluated (See: zvon.org tutorial p26)zvon.org tutorial p26 Hello would insert the text Hello into the result tree if is a child of the context node Interesting example: zvon.org tutorial p28zvon.org tutorial p28 is more flexible (See: zvon.org tutorial p27)zvon.org tutorial p27 good ok hmmm

25 Dickson Chiu-2004CSIT600b 02-25 Often we need to process a number of nodes in series + a new template would work is an (better) alternative, which forms a template within a template Hello, ! See: zvon.org tutorial p18zvon.org tutorial p18

26 Dickson Chiu-2004CSIT600b 02-26 and takes sections (sub-structure) of the source tree and copies them to the result tree (See: zvon.org tutorial p63) zvon.org tutorial p63 vs. manually using a series of ’s Uses the select attribute to indicate what to copy is more flexible (can set attributes at the same time, see: p64)p64

27 Dickson Chiu-2004CSIT600b 02-27 To apply to and, using the select attribute to choose the key See: zvon.org tutorial p19zvon.org tutorial p19 Other attributes include data-type (text* / number, see: p20 ), order (ascending* / descending), and case-order (upper-first* / lower-first, see: p21 ) p20 p21 * means default

28 Dickson Chiu-2004CSIT600b 02-28 Variables and Constants As in programming languages 3.14 which defines a constant to be denoted by $pi which defines a variable, $who BUT: no variable update – you can only put updated value into new variables See: zvon.org tutorial p32zvon.org tutorial p32 can contain XML markup (see: p35)p35 …

29 Dickson Chiu-2004CSIT600b 02-29 Other functions String functions (see: zvon.org tutorial p47-52) concat(), starts-with(), substring-before(), substring-after(), string-length(), normalize-space(), translate()… Node set functions count() (p54), position(), last() (p53)p54p53 Numeric calculations (p38-42) ceilng(), floor(), round(), /, div … Test for number (p42): is not a number p42xsl:iftestxsl:if Boolean functions (e.g., p44)p44 xsl:templatematch Output Coding (p58-62) xsl:outputmethodencoding

30 Dickson Chiu-2004CSIT600b 02-30 XSLT vs. CSS CSS pushes, XSLT pulls CSS is content-blind and order-sensitive CSS is not a programming language, XSLT is like one But CSS provides a rich set of capabilities for content presentation, XSLT has nothing on styling (XSL FO has) CSS should still be considered an integral part of using XML at least until XSL FO becomes stable and widely available; even then, CSS is an easier alternative

31 Dickson Chiu-2004CSIT600b 02-31 XSLT and XSL FO The use of XSL in two steps:

32 Dickson Chiu-2004CSIT600b 02-32 F Objects and Properties Formatting is enabled by including formatting semantics (in terms of formatting objects and properties) in the result tree Formatting in 3 steps: To “objectify” the element and attribute tree from XSLT into formatting objects To refine the formatting object tree to produce the refined formatting object tree Construction of the area tree, where each geometric area has a position on the page

33 Dickson Chiu-2004CSIT600b 02-33 Benefits of XSL FO XSL builds on the prior work on Cascading Style Sheets XSL introduces a model for pagination and layout that extends what is currently available XSL uses XSLT and XPath for tree construction and pattern selection, even where mixed namespaces are involved A comprehensive area model of which the CSS2 box model is a subset Internationalization and writing modes

34 Dickson Chiu-2004CSIT600b 02-34 Status of XSL FO http://www.w3.org/Style/XSL/XSL-FO/ W3C Recommendation 15 October 2001 Software Microsoft is behind: MSXML supports only the XSLT part of XSL Apache’s FOP RenderX’s XEP


Download ppt "1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. Francis Lau (HKU) CSIT600b: XML Programming XPath, XSL / XSLT."

Similar presentations


Ads by Google