Overview of XPath 1 XPATH XPath expressions are

  • Slides: 11
Download presentation
Overview of XPath 1

Overview of XPath 1

XPATH • XPath expressions are used to locate nodes in XML documents • The

XPATH • XPath expressions are used to locate nodes in XML documents • The list of nodes located by an XPath expression is called a Nodelist • XPath is used in XSL and in XQuery (a query language for XML) • W 3 Schools has an XPath tutorial 2

Using XPATH in XSL • Xpath expressions are used in XSL, for example Ø<xsl:

Using XPATH in XSL • Xpath expressions are used in XSL, for example Ø<xsl: for-each select=“xpath-expression”> • Iteration over the nodes selected by the XPath expression Ø<xsl: value-of select=“xpath-expression”> • Selecting the value of a node Ø<xsl: if test=“xpath-expression”> • The test is true if some node is located by (i. e. , satisfies) the XPath expression 3

Examples of XPath Expressions • Para Ø Selects the para children elements of the

Examples of XPath Expressions • Para Ø Selects the para children elements of the context node • * Ø Selects all element children of the context node • text() Ø Selects all text node children of the context node • @name Ø Selects the name attribute of the context node 4

More Examples of XPath Expressions • @* Ø Selects all the attributes of the

More Examples of XPath Expressions • @* Ø Selects all the attributes of the context node • para[1] Ø Selects the first para child of the context node • para[last()] Ø Selects the last para child of the context node • */para Ø Selects all para grandchilren of the context node 5

More Examples of XPath Expressions • /doc/chapter[5]/section[2] Ø Selects the second section of the

More Examples of XPath Expressions • /doc/chapter[5]/section[2] Ø Selects the second section of the fifth chapter of the doc • chapter//para Ø Selects the para element descendants of the chapter element children of the context node • //para Ø Selects all the para descendants of the document root and thus selects all para elements in the same document as the context node 6

More Examples of XPath Expressions • //olist/item Ø Selects all the item elements that

More Examples of XPath Expressions • //olist/item Ø Selects all the item elements that have an olist parent and are in the same document as the context node • . Ø Selects the context node • . //para Ø Selects the para descendants of the context node • . . Ø Selects the parent of the context node 7

More Examples of XPath Expressions • . . /@lang Ø Selects the lang attribute

More Examples of XPath Expressions • . . /@lang Ø Selects the lang attribute of the parent of the context node • para[@type=“warning”] Ø Selects the para children of the context node that have a type attribute with value warning • Chapter[title] Ø Selects the chapter children of the context node that have one or more title children 8

More Examples of XPath Expressions • para[@type=“warning”][5] ØSelects the fifth para child among the

More Examples of XPath Expressions • para[@type=“warning”][5] ØSelects the fifth para child among the children of the context node that have a type attribute with value warning • Para[5][@type=“warning”] ØSelects the fifth para child of the context node if that child has a type attribute with value warning 9

More Examples of XPath Expressions • Chpater[title=“Introduction”] ØSelects the chapter children of the context

More Examples of XPath Expressions • Chpater[title=“Introduction”] ØSelects the chapter children of the context node that have one or more title children with string-value equal to Introduction • employee[@secretary and @assistant] ØSelects employee children of the context node that have both a secretary attribute and an assistant attribute 10

Location Paths • The previous examples are abbreviations of location paths Ø See XPath

Location Paths • The previous examples are abbreviations of location paths Ø See XPath tutorial in W 3 Schools or Costello’s slides on long names • For example, // is short for /descendantor-self: : node()/. • //para is short for /descendant-or-self: : node()/child: : para 11