Download presentation
Presentation is loading. Please wait.
Published byAbigayle Bradley Modified over 8 years ago
1
1 XPath Extracting Data from XML
2
2 Data stored in an XML document must be extracted to use with various applications Data can be extracted programmatically We will discuss XPath: a declarative language for extracting data from XML XPath is used extensively in other specifications, e.g., XQuery, XSL, XPointer
3
3 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 An XML document
4
4 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK USA Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 The XML document as a DOM tree The XML document as a DOM tree
5
5 Main Element of Syntax: Path Expressions / at the beginning of an XPath expression represents the root of the document / between element names represents a parent-child relationship // represents an ancestor-descendent relationship @ marks an attribute [condition] specifies a condition
6
6 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog Getting the root element of the document USA
7
7 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd Finding child nodes USA
8
8 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd/price Finding descendent nodes USA
9
9 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd[price<10] Condition on elements USA
10
10 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 //title // represents any directed path in the document /catalog//title USA
11
11 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd/* * represents any element name in the document USA
12
12 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /*/* What will the following expressions return? //* //*[price=9.90] //*[price=9.90]/* USA * represents any element name in the document
13
13 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd[1] Position based condition /catalog/cd[last()] USA
14
14 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 //title | //price Use | for union USA
15
15 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 /catalog/cd[@country=“UK”] @ marks attributes USA
16
16 catalog.xml catalog cd country titleartistprice titleartistpricetitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 @ marks attributes USA /catalog/cd/@country
17
17 catalog.xml catalog cd country titleartistprice titleartisttitleartistprice country UK Dark Side of the Moon Space OddityAretha: Lady Soul Pink Floyd David BowieAretha Franklin 10.90 9.90 USA price How would you write: The price of the cds Whose artist is David Bowie?
18
18 XPath Axises We have discussed the following axises: Child (/) Descendent (//) Attribute (@) These symbols are actually a shorthand, e.g., /cd//price is the same as child::cd/descendent::price There are additional shorthands, e.g., Self (/.) Parent (/..)
19
19 Additional Axises ancestorContains all ancestors (parent, grandparent, etc.) of the current node ancestor-or-selfContains the current node plus all its ancestors (parent, grandparent, etc.) descendant-or-selfContains the current node plus all its descendants (children, grandchildren, etc.) followingContains everything in the document after the closing tag of the current node following-siblingContains all siblings after the current node precedingContains everything in the document that is before the starting tag of the current node preceding-siblingContains all siblings before the current node
20
20 More Details XPath specification at W3C XPath XPath tutorial in W3Schools XPath tutorial Mulberry XPath Quick ReferenceQuick Reference
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.