d8ec56e58585d524de144451a696ce78.ppt
- Количество слайдов: 48
/*/*/self: : * XPath Dongwon Lee, Ph. D. IST 516 Fall 2011
XPath l Path-based XML query language l l V 1. 0 – 1999: http: //www. w 3. org/TR/xpath V 2. 0 – 2003: http: //www. w 3. org/TR/xpath 20/ Functional, strongly-typed query language http: //www. w 3 schools. com/xpath_intro. asp 2
Apps of XPath l XQuery: a full-blown query language for XML for $x in doc("books. xml")/bookstore/book where $x/price>30 order by $x/title return $x/title l XPointer/XLink: a standard way to create hyperlinks in XML
Apps of XPath l XSLT: a style sheet language of XML that can transform XML from one to another format
XPath vs. SQL XPath l l XML Model Trees Hierarchy Order SQL l l Relational Model Tables Flat Orderless (except ORDER-BY) 5
XPath vs. XQuery XPath l l XML Model Trees Hierarchy Order XQery l l Can do all XPath does but not vice versa Turing-Complete general purpose PL Can retrieve, update, and transform XML data FLWOR expression 6
XPath Expression l Expression (basic building block) returns one of the 4 objects: 1. node-set (an unordered collection of nodes without duplicates). . . 2. boolean (true or false) number (a floating-point number) string (a sequence of characters) 3. 4. 7
XPath Nodes processing-instruction xml version="1. 0” encoding="UTF-8”? >
Location Step l l l Location Steps are evaluated in order from left to right Preferred – Absolute: /step/… Faster to evaluate Relative: step/… axis : : node-test [predicate] l l l Axis: Specifies the node relationship Node Test: specifies node type and name Predicate: Instructions to filter nodes 9
1. Axis l / selects the root of the node hierarchy l l Forward Axis l l child: : , descendent: : , attribute: : , self: : , descendentor-self: : , following-sibling: : , following: : Backward Axis l l
Node Relationships Courses Self Undergrad Ancestors Graduate Parent Sibling Descendants Room Name Instructor Office Child Phone Grandchild 11
1. Axis Abbreviation l l l Descendent-or-self: : node() // child: : / attribute: : @ self: : node() . parent: : . . Eg l l /child: : doc/descendent: : chapter /doc//chapter //doc/attribute: : type //doc/@type 12
2. Node Test l l l node(): matches all nodes text(): matches all text nodes Element. Name: matches all elements of type ‘Element. Name’ *: matches all elements @*: matches all attributes 13
2. Node Test l * (wildcard) is often used to match unknown XML elements l /catalog/cd/*: all the child elements of all the cd elements of the catalog element /* : all children of the root
3. Predicate l l Path-expresson[ filtering condition ] Pathexpression that satisfies the filtering condition Eg l l l //doc [@type=‘PDF’] finds all
Location Step Examples 16
Examples of usage 17
IST Example What IST Classes are in Room IST 110? /Courses/*[child: : Room=‘ 110 IST’] Original XML Result
IST Example What IST courses have TA’s? /Courses/*/TA/parent: : * Original XML Result
IST Example What rooms are used by IST courses? /Courses/*/Room/text() Original XML Result
Comparison l Comparison can be performed using l l =, !=, <, >=, and > Examples l l [child: : Room != ‘ 205 IST’] [child: : Time > 1220] NOTE: When used within Predicate, Child: : Room == Child: : Room/text() 21
Math Operators l + : performs addition - : performs subtraction * : performs multiplication div : performs division mod : returns the remainder of division l Examples: l l l [child: : Time mod 100 = 30] 22
Node Functions l l last() : returns the numeric position of the last node in a list position() : returns the numeric position of the current node count() : returns the number of nodes in a list name(): returns the name of a node id() : selects elements by their unique ID 23
Node Function Example Which courses have more than 2 child elements? /Courses/*[count(child: : *)>2] Original XML Result
String Functions l l concat(string, string) : concatenates the string arguments starts-with(string, string) : returns true if the first string starts with the second string contains(string, string) : returns true if the first string contains the second string Eg, l concat(‘sh’, ‘oe’) = ‘shoe’ l starts-with(‘cat’, ‘ca’) = true l contains(‘puppy’, ‘upp’) = true 25
String Functions l l l substring(string, number, [number]) : returns a substring of the provided string-length(string) : returns the number of characters in the string Eg, l l l substring(‘chicken’, 3, 4) = ‘icke’ substring(‘chicken’, 3) = ‘icken’ string-length(‘cat’) = 3 26
String Functions Examples l //Book [startswith(child: : Title, “X”)] / price l //Book [stringlength(Author/FN)=3] / Title
Number Functions l sum(node-set) : returns the sum of values for each node in a node set l l floor(number) : returns the largest integer that is not greater than the argument l l Eg, floor(2. 6) = 2 ceiling(number) : returns the smallest integer that is not less than the argument l l Eg, sum(//@price) Eg, ceiling(2. 6) = 3 round(number) : returns the closest integer to the argument l Eg, round (2. 4) = 2 28
Boolean OPs in XPath l Conjunction: “and” l l Disjunction: ““or” l l /Customer[@cname=‘Lee’ or @cid>100] Disjunction: “|” l l l //Product[@price>10. 8 and @year>2000] Compute both node-sets and return the union //Book | //Tape NOTE: some XPath engines currently support only either “|” or “or” disjunction 29
XPath Lab [www. zvon. org] l /AAA/CCC
XPath Lab [www. zvon. org] l //BBB
XPath Lab [www. zvon. org] l /AAA/BBB[1]
XPath Lab [www. zvon. org] l /AAA//BBB[1]
Position Explanation l “/AAA//BBB” returns two lists: l l l Three
XPath Lab [www. zvon. org] l //*[count(BBB)=2]
XPath Evaluation S/W l l Many S/W have built-in support for XPath 1. 0 and 2. 0 now Eg, l XPath Visualizer: Windows only http: //xpathvisualizer. codeplex. com/ l l l XMLSpy: Windows only
#1. XPath Visualizer Answer #2 for //letter/paragraph Answer #1 for //letter/paragraph Minor bug here 38
#2. XMLSpy Choose Evaluate XPath 39
#2. XMLSpy Answer #1 for //letter/paragraph 40
#2. XMLSpy Answer #2 for //letter/paragraph 41
#3.
#3.
#4 XMLPad 44
XPath Evaluation in Programming l XPath Engines / Libraries l l Apache Xalan-Java: http: //xml. apache. org/xalan-j/ Saxon: http: //saxon. sourceforge. net/ Jaxen: http: //jaxen. codehaus. org/ PL specific APIs l l Java: package javax. xml. xpath + DOM PHP: domxml’s xpath_eval() (v 4), Simple. XML (v 5)
Eg. XPath in JAVA public Node find. Address(String name, Document source) throws Exception { // need to recreate a few helper objects XMLParser. Liaison xpath. Support = new XMLParser. Liaison. Default(); XPath. Processor xpath. Parser = new XPath. Processor. Impl(xpath. Support); Prefix. Resolver prefix. Resolver = new Prefix. Resolver. Default(source. get. Document. Element()); // create the XPath and initialize it XPath xp = new XPath(); String xp. String = "//address[child: : addressee[text() = '” +name+"']]"; xpath. Parser. init. XPath(xp, xp. String, prefix. Resolver); // now execute the XPath select statement XObject list = xp. execute(xpath. Support, source. get. Document. Element(), prefix. Resolver); return list. nodeset(). item(0); } 46 http: //www. javaworld. com/javaworld/jw-09 -2000/jw-0908 -xpath. html? page=3
Using direct" src="https://present5.com/presentation/d8ec56e58585d524de144451a696ce78/image-47.jpg" alt="Eg. Simple. XML in PHP php $xml = simplexml_load_file('employees. xml'); echo "Using direct" />
Eg. Simple. XML in PHP php $xml = simplexml_load_file('employees. xml'); echo "Using direct method. . .
"; $names = $xml->xpath('/employees/employee/name'); foreach($names as $name) { echo "Found $name
"; } echo "
"; echo "Using indirect method. . .
"; $employees = $xml->xpath('/employees/employee'); foreach($employees as $employee) { echo "Found {$employee->name}
"; } echo "
"; echo "Using wildcard method. . .
"; $names = $xml->xpath('//name'); foreach($names as $name) { echo "Found $name
"; } ? > http: //www. tuxradar. com/practicalphp/12/3/3 47
Lab #2 (DUE: Sep. 25 11: 55 PM) l https: //online. ist. psu. edu/ist 516/labs l Tasks: l l l Individual Lab Using an XML files, practice XPath queries Turn-In l l XPath queries and English interpretation Screenshot of results of XPath queries 48


