Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.monash.edu.au CSE3201/CSE4500 XPath. www.monash.edu.au 2 XPath A locator for items in XML document. XPath expression gives direction of navigation.

Similar presentations


Presentation on theme: "Www.monash.edu.au CSE3201/CSE4500 XPath. www.monash.edu.au 2 XPath A locator for items in XML document. XPath expression gives direction of navigation."— Presentation transcript:

1 www.monash.edu.au CSE3201/CSE4500 XPath

2 www.monash.edu.au 2 XPath A locator for items in XML document. XPath expression gives direction of navigation in XML document. Assume an XML document as a “tree” Any part of a document, eg element, attribute, is considered as a “node” Current version XPATH 1.0

3 www.monash.edu.au 3 XPath Syntax (full form): axis :: node-test [predicate] Axis –describing the relationship between nodes, eg child, parents, etc. Node test –condition for selecting nodes. Predicate: –further condition refinement of the set of nodes resulted from the node test.

4 www.monash.edu.au 4 XPath Axes Ancestor Parent/ancestor sibling node child/descendant descendant attribute sibling context node

5 www.monash.edu.au 5 Node Test A node test identifies nodes in the document that meet the criteria of the test. The simplest type of test is nodes that match an element name. Example: child::book => to find any child element with the name “book”. child::author

6 www.monash.edu.au 6 Predicate Predicate further refine or filter the node-set produced by the node test. Example: –Find the third book in the list >child::book[position( )=3] –Find all the books that has element >child::book[isbn]

7 www.monash.edu.au 7 Abbreviations FormalShortDescription child::bookbookSelect all children of the context node that has element nodes. child::**Select all element nodes of the context node. self::node().Select the context node. parent::node()..Select the parent of the context node. child::book[positi on()=1] Book[1]Select the first child element that has element. attribute::*@*select all the attributes of the context node attribute::number@numberFind the number of attributes in the context node.

8 www.monash.edu.au 8 Location Path Document Root “John” “Little” “Howard” /name/first Uses “/” to build path, eg

9 www.monash.edu.au 9 Relative vs Absolute Path Absolute Path –full path needs to be included, starting from the root node. >eg: /name/first Relative Path –path is declared starting from the current context node. >eg: assume our current context is “name”, the XPath expression for the node first => first

10 www.monash.edu.au 10 Recursive Decent Operator Locating nodes based on their names, regardless of where their positions in the document. Uses “//” Example: //first –Select any element in the document (regardless how far down the tree). Decrease the performance of the stylesheet. –The entire document must be searched by the XSLT parser.

11 www.monash.edu.au 11 Filtering Nodes It is done using XPath’s predicate. –the “[ ]” symbol. Using element as a filter: –book[price] matches any element that has a child element. Using attribute as a filter: –book[@id] matches any element that has an id attribute.

12 www.monash.edu.au 12 XPath Expression Some possible operators to build an XPath Expression: andLogical AND orLogical OR not()logical negation =Equal !=Not equal <Less than <=Less than equal >Greater than >=Greater than equal |Union

13 www.monash.edu.au 13 XPath Expression - Examples

14 www.monash.edu.au 14 Usage of XPath in XSLT XSLT uses XPath expression to: –Match node sets in order to execute templates. –Evaluate node sets to control execution of conditional XSLT elements. –Select node sets to change current context and direct the flow of the execution through the source document. –Select node sets to obtain an output value Professional XML, page 379.

15 www.monash.edu.au 15 XPath Function XPath functions can be used to: –manipulate node set >eg: count, last, name, position –manipulate string >eg: concat, substring, contains –test boolean value >eg: language, false, true –perform numeric operations >eg: ceiling, floor, number, round, sum –XSLT specific manipulation >eg: current

16 www.monash.edu.au 16 XPath Function - Examples substring(‘abcde’,2,3) => returns ‘bcd’


Download ppt "Www.monash.edu.au CSE3201/CSE4500 XPath. www.monash.edu.au 2 XPath A locator for items in XML document. XPath expression gives direction of navigation."

Similar presentations


Ads by Google