xpath
play

XPath XPath is a language for describing paths in XML documents. - PDF document

XPath XPath is a language for describing paths in XML documents. XML query languages Think of an SSD graph and its paths. Path descriptors are similar to path descriptors in a (UNIX) file system. XPath A simple path descriptor


  1. XPath • XPath is a language for describing paths in XML documents. XML query languages – Think of an SSD graph and its paths. • Path descriptors are similar to path descriptors in a (UNIX) file system. XPath – A simple path descriptor is a sequence of element names separated by slashes (/). XQuery – / denotes the root of a document. – // means the path can start anywhere in the tree from the current node. Examples: More path descriptors <Courses> <Course name=”Databases” code=”TDA357”> • There are other path descriptors than / and //: <GivenIn period=”2” teacher=”Niklas Broberg” /> <GivenIn period=”4” teacher=”Rogardt Heldal” /> – * denotes any one element: </Course> • /Courses/*/* will give all children of all children of a <Course name=”Algorithms” code=”TIN090”> Courses element, i.e. all GivenIn elements. <GivenIn period=”1” teacher=”Devdatt Dubhashi” /> • //* will give all elements anywhere. </Course> </Courses> – . denotes the current element: • /Courses/Course/. will return the same elements as /Courses/Course /Courses/Course/GivenIn will return the set of all – .. denotes the parent element: GivenIn elements in the document. • //GivenIn/.. will return all elements that have a //GivenIn will return the same set, but only since we know by GivenIn element as a child. our schema that GivenIn elements can only appear in that • Think about how we can traverse the graph – position. upwards, downwards, along labelled edges etc. /Courses will return the document as it is. Attributes Axes • Attributes are denoted in XPath with a @ • The various directions we can follow in a symbol: graph are called axes (sing. axis) . – /Courses/Course/@name will give the • General syntax for following an axis is names of all courses. axis :: – Example: /Courses/child::Course Quiz: For the Scheduler example, what will • Only giving a label is shorthand for the path expression //@name result in? child::label , while @ is short for attribute:: The names of all courses, and the names of all rooms. 1

  2. More axes Selection • Some other useful axes are: • We can perform tests in XPath – parent:: = parent of the current node. expressions by placing them in square • Shorthand is .. brackets: – descendant-or-self:: = the current node(s) and all – /Courses/Course/GivenIn[@period = 2] will descendants (i.e. children, their children, …) down give all GivenIn elements that regard the second through the tree. period. • Shorthand is // Quiz: What will the path expression – ancestor::, ancestor-or-self = up through the tree /Courses/Course[GivenIn/@period = 2] – following-sibling:: = any elements on the same level result in? that come after this one. All Course elements that are given in the second period (but for each – … of those, all the GivenIn elements for that course). Example: /Courses/Course[GivenIn/@period = 2] Quiz! TIN090 TDA357 Write an XPath expression that gives the Courses course course code code courses that are given in period 2, but with alg name db name only the GivenIn element for period 2 as a Databases givenIn 2 givenIn Algorithms child! givenIn p2 period period p4 4 It can’t be done! nrStudents nrStudents period teacher 1 XPath is not a full query language, it only allows us p1 120 teacher 138 to specify paths to elements or groups of elements. Niklas Broberg teacher Rogardt Heldal nrStudents We can restrict in the path using [ ] notation, but we Devdatt Dubhashi cannot restrict further down in the tree than what the path points to. 68 XQuery XQuery “Hello World” • XQuery is a full-fledged querying language If our XQuery file contains: for XML documents. <Greeting>Hello World</Greeting> – Cf. SQL queries for relational data. or: • XQuery is built on top of XPath, and uses let $s := "Hello World" return <Greeting>{$s}</Greeting> XPath to point out element sets. then the XQuery processor will produce the • XQuery is a W3 recommendation. following XML document: <?xml version="1.0" encoding="UTF-8"?> <Greeting>Hello World</Greeting> 2

  3. Function doc("file.xml") Quiz! bash$ cat example.xq Write an XQuery expression that puts extra doc("courses.xml") <Result></Result> tags around the result, e.g. bash$ xquery example.xq <?xml version="1.0" encoding="UTF-8"?> <Courses> <Result> <Courses> <Course name="Databases" code="TDA357"> <Course name="Databases" code="TDA357"> <GivenIn period="2" teacher="Niklas Broberg"/> <GivenIn period="2" teacher="Niklas Broberg"/> <GivenIn period="4" teacher="Rogardt Heldal"/> <GivenIn period="4" teacher="Rogardt Heldal"/> </Course> </Course> <Course name="Algorithms" code="TIN090"> <Course name="Algorithms" code="TIN090"> <GivenIn period="1" teacher="Devdatt Dubhashi"/> <GivenIn period="1" teacher="Devdatt Dubhashi"/> </Course> </Course> </Courses> </Courses> </Result> Putting tags around the result FLWOR Curly braces are necessary to evaluate the • Basic structure of an XQuery expression is: expression between the tags. – FOR-LET-WHERE-ORDER BY-RETURN. – Called FLWOR expressions (pronounce as flower ). <Result>{doc("courses.xml")}</Result> • A FLWOR expression can have any number of FOR (iterate) and LET (assign) clauses, possibly Alternatively, we can use a let clause to assign a mixed, followed by possibly a WHERE clause value to a variable. Again, curly braces are and possibly an ORDER BY clause. needed to get the value of variable $d. • Only required part is RETURN. let $d := doc("courses.xml") return <Result>{$d}</Result> Quiz! Quiz! What does the following XQuery expression What does the following XQuery expression compute? compute? let $courses := doc("courses.xml") let $courses := doc("courses.xml") for $gc in $courses//GivenIn let $gc := $courses//GivenIn[@period = 2] where $gc/@period = 2 return <Result>{$gc}</Result> return <Result>{$gc}</Result> <?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?> <Result> <Result> <GivenIn period="2" teacher="Niklas Broberg"/> <GivenIn period="2" teacher="Niklas Broberg"/> </Result> </Result> 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend