9 path expressions xpath
play

9. Path expressions: XPath XPath is a language for selecting parts - PowerPoint PPT Presentation

9. Path expressions: XPath XPath is a language for selecting parts of XML documents it is a kind of simple query language . XPath does not use normal XML syntax; path expressions are strings for the parser. XPath is a tree


  1. 9. Path expressions: XPath • XPath is a language for selecting parts of XML documents – it is a kind of simple query language . • XPath does not use normal XML syntax; path expressions are strings for the parser. • XPath is a tree traversal (’navigation’) language, with techniques for moving from one node to another in the document tree, and for restricting the set of selected nodes. • XPath is used e.g. by XML Schema , XSLT , XLink , XPointer , XForms , ... XML-9 J. Teuhola 2013 151

  2. Node types in the document tree • Root node (with special interpretation) • Element nodes • Text nodes • Attribute nodes • Processing instruction nodes • Namespace nodes ( ≠ normal attributes) • Comment nodes Note. DTD, CDATA, and entity references are assumed to have been merged to the document before applying XPath to the document tree. XML-9 J. Teuhola 2013 152

  3. Path expressions • Called location paths • One path identifies zero, one or more nodes (attributes, elements, etc.) in the document tree. • A location path consists of location steps . • Each step continues from a context node , produced by the previous step (e.g. match). • Path notation resembles Unix directory paths: – Root = ”/”; has as children the actual root element and the stuff before the root element (processing instructions and comments) – Paths can be absolute (starting from the root ”/...”), or relative to the current subtree. XML-9 J. Teuhola 2013 153

  4. Location step types • Element name moves from the context node to the child elements with the given name. • Attribute name , prefixed by ’ @ ’, selects the named attribute of the context node. • text () matches text nodes within the context node, i.e. maximum possible text segments. • comment () matches the comment nodes under the context node. • processing-instruction () matches the processing instructions under the context node. XML-9 J. Teuhola 2013 154

  5. Example document: course list <?xml version=”1.0”?> <courses> <course cname=”Advanced databases”> <teacher>Jukka</teacher> <audience> <student name=”Pekka”/> <student name=”Pirkko”/> </audience> </course> <course cname=”Medical informatics”> <teacher>Timo</teacher> <audience> <student name=”Pekka”/> <student name=”Paula”/> </audience> </course> </courses> XML-9 J. Teuhola 2013 155

  6. Element and attribute location steps for listing course names with XSLT <?xml ... ?> <xsl:stylesheet ... > <!-- Start with an absolute location step --> <xsl:template match=“/courses”> <html> <head><title>XPath test</title></head> <!-- Perform a location step relative to ‘/courses’ --> <body> <xsl:apply-templates select=“course”/> </body> </html> </xsl:template> <xsl:template match="course"> Result: <!-- L ocation step relative to ‘course’ --> Advanced databases <xsl:value-of select="@cname"/> Medical informatics </xsl:template> </xsl:stylesheet> XML-9 J. Teuhola 2013 156

  7. Extensions of location steps • Wildcards: – ’ ∗ ’ matches any element node being a child of the context node, irrespective of the name. – ’prefix: ∗ ’ matches all child elements in the given namespace. – ’node () ’ matches all nodes - attributes with ’@node ()’. – ’@ ∗ ’ matches all attribute nodes – ’@prefix: ∗ ’ matches all attribute nodes in the given namespace. • Alternatives: separated by ’|’. – ’a|b’ matches all child nodes with name a or b. XML-9 J. Teuhola 2013 157

  8. Example: Print child elements & attributes Document: <?xml version="1.0" encoding="UTF-8"?> <example type="small" name=“node printing example"> <greeting>Hello world!</greeting> </example> Stylesheet: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=“…"> <xsl:template match=“/example"> <html> <head /> <body> <xsl:for-each select=“*|@*"> <xsl:value-of select="."/> <br /> </xsl:for-each> Result (browser view): </body> small </html> node printing example </xsl:template> Hello world! </xsl:stylesheet> XML-9 J. Teuhola 2013 158

  9. Combining location steps • Combining operator: ’/’ (cf. disk directory paths) • In a compound path ’a/b/c/...’ step a is with respect to the current context node; for b, c, ... the context is the result of the previous step. • At all steps, the result is a set of nodes. • Special cases: – Starting from the root: ’/a/b/c/...’ – Selecting all descendants: ’//’ – Selecting the context node: ’.’ – Selecting the parent of the context node: ’..’ XML-9 J. Teuhola 2013 159

  10. Example: compound path for listing students of all courses <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <xsl:for-each select="courses/course/audience/student"> <xsl:value-of select=“@name"/> <br /> </xsl:for-each> Result (browser view): </body></html> Pekka Pirkko </xsl:template> Pekka </xsl:stylesheet> Paula XML-9 J. Teuhola 2013 160

  11. Selection conditions • At any location step, one can restrict the selected set by giving a predicate . • The predicate is a boolean expression in [ ... ]. Only nodes satisfying it are selected. • The predicate may contain normal comparison and Boolean operators. • The operands may be arbitrary components (using XPath) with respect to the context node. • Other data types can be interpreted as Boolean values by type casting (function boolean ()). XML-9 J. Teuhola 2013 161

  12. Example path with conditions: students of ’Medical Informatics’ <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <xsl:for-each select="courses/course[@cname= 'Medical informatics']/audience/student"> <xsl:value-of select="@name"/> </xsl:for-each> </body></html> Result (browser view): </xsl:template> Pekka </xsl:stylesheet> Paula XML-9 J. Teuhola 2013 162

  13. Unabbreviated notation • The ’direction’ of location can be expressed by a so called axis . • Syntax: ’axis::node ’ . • More powerful than the abbreviated syntax, but not much used. • Axis types: – parent, child – descendant, descendant-or-self – ancestor, ancestor-or-self – following, preceding (in document order) – following-sibling, preceding-sibling (in document order) – attribute – self XML-9 J. Teuhola 2013 163

  14. Example of explicit axis notations: navigate to students in the ‘courses’-tree <!-- See slide 155 for content of the ‘courses’ document --> <xsl:template match= "/child::courses "> <html> <body> <!-- Navigate to the ‘audience’ node --> <xsl:for-each select=" child::course/child::teacher/following-sibling::* "> <!-- scan the students of the audience --> <xsl:for-each select=" child::student "> <!-- Pick up the names of students --> <xsl:value-of select=" attribute::name "/><br/> </xsl:for-each> Result (browser view): </xsl:for-each> Pekka </body> Pirkko </html> Pekka </xsl:template> Paula XML-9 J. Teuhola 2013 164

  15. Other types of XPath expressions • Above expressions are of type nodeset ; others: • Numbers : – double-precision (8-byte) floating-point numbers; used also for integers, – normal arithmetic operations (+, -, *, div, mod) • Strings : – sequences of Unicode characters; some syntactic restrictions depending on the context, – (in)equality comparison available (=, !=), less/greater meaningful only for numeric strings, • Booleans : – Results from comparisons and Boolean operations. XML-9 J. Teuhola 2013 165

  16. XPath functions for nodesets • position (): the relative position of the current node within the context node list; used mainly in XSLT template rules. • last (): number of nodes in the context • count (x): number of nodes in the argument • id (x): nodeset having the argument IDs. • local-name ( x ): local part of the namespace of the first node in the argument. • namespace-uri ( x ): as above, but URI returned • name ( x ): returns the prefixed name of the first node in the argument. XML-9 J. Teuhola 2013 166

Recommend


More recommend