Download presentation
Presentation is loading. Please wait.
1
{ XML Technologies } BY: DR. M’HAMED MATAOUI
Chapter V: Addressing and Querying XML Documents
2
Overview of Presentation
Introduction to XML Technologies Fundamental Concepts of XML XML Grammars (DTD, W3C schema) XML document transformations (CSS, XSLT) Addressing and Querying XML Documents XPath XQuery Processing XML (DOM & SAX) Overview of Presentation [ Dr. M’hamed MATAOUI ]
3
Goals Learn how to address (navigate) XML elements by using XPath Notation. Understand and Use XQuery language for querying XML data Goals [ Dr. M’hamed MATAOUI ]
4
XPath XPath is a syntax for defining parts of an XML document
XPath (XML Path Language) is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C). XPath is used primarily for selecting parts of an XML document. For this purpose the XML document is modelled as a tree of nodes. XPath allows nodes to be selected by means of a hierarchic navigation path through the document tree. XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in the XSLT standard XPath is a W3C recommendation From : Wikipedia From : XPath [ Dr. M’hamed MATAOUI ]
5
XPath XPath Path Expressions XPath
XPath uses path expressions to select nodes or node-sets in an XML document. These path expressions look very much like the expressions used in a traditional computer file system: Example: /BOOKS/BOOK/AUTHOR/LASTNAME In general, an XPath expression specifies a pattern that selects a set of XML nodes. (from : docs.oracle.com) From : XPath [ Dr. M’hamed MATAOUI ]
6
XPath XPath XPath Standard Functions
XPath includes over 100 built-in functions: string values, numeric values, booleans, date and time comparison, node manipulation, sequence manipulation, and much more. Today XPath expressions can also be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages. XPath is Used in XSLT XPath is a major element in the XSLT standard. With XPath knowledge you will be able to take great advantage of your XSLT knowledges. XPath and XQuery 3.0 XPath is a major element in the XSLT standard. With XPath knowledge you will be able to take great advantage of your XSLT knowledges. From : XPath [ Dr. M’hamed MATAOUI ]
7
XPath XPath Nodes The nodes in an XPath expression refer to more than just elements. They also refer to text and attributes, among other things. In fact, the XPath specification defines an abstract document model that defines seven kinds of nodes: Root Element Text Attribute Comment Processing instruction Namespace XPath [ Dr. M’hamed MATAOUI ]
8
XPath XPath Relationship of Nodes Parent
Each element and attribute has one (and only one) parent. Children Element nodes may have zero, one or more children. Siblings Nodes that have the same parent. Ancestors A node's parent, parent's parent, etc. Descendants A node's children, children's children, etc. XPath [ Dr. M’hamed MATAOUI ]
9
XPath XPath Syntax XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps. The most useful path expressions are: Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! XPath [ Dr. M’hamed MATAOUI ]
10
XPath XPath [ Dr. M’hamed MATAOUI ]
11
XPath XPath Select Node by indicating it’s « Index »
[ Dr. M’hamed MATAOUI ]
12
XPath XPath [ Dr. M’hamed MATAOUI ]
13
XPath OR XPath [ Dr. M’hamed MATAOUI ]
14
XPath XPath [ Dr. M’hamed MATAOUI ]
15
XPath XPath [ Dr. M’hamed MATAOUI ]
16
XPath XPath [ Dr. M’hamed MATAOUI ]
17
XPath Reaching unknown nodes
Predicates XPath wildcards can be used to select unknown XML elements. Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets. Wildcards = Caractères génériques XPath [ Dr. M’hamed MATAOUI ]
18
XPath XPath Operators Operator Meaning [] predicate /, //
child nodes, descendant nodes | union *, div, mod multiply, divide, modulo +, - plus, minus <, <=;, >, >=; Less-than, less-or-equal, greater-than, greater-or-equal = equals and Boolean and or Boolean or XPath Operators XPath [ Dr. M’hamed MATAOUI ]
19
XPath An XPath expression returns either a node-set, a string, a Boolean, or a number. XPath Operators XPath [ Dr. M’hamed MATAOUI ]
20
XPath XPath Axes An axis defines a node-set relative to the current node. XPath [ Dr. M’hamed MATAOUI ]
21
following is not following-sibling
XPath Examples: /course/plan/ancestor::node()/Lecturer /course/plan/ancestor-or-self::node() /course/Lecturer/attribute::* /course/child::node()/comment() /course/descendant::node()[4]/child::node()[4] /course/plan/chapter[1]/following-sibling::node()[2] XPath Axes An axis defines a node-set relative to the current node. following is not following-sibling XPath [ Dr. M’hamed MATAOUI ]
22
XPath XPath Node Set Functions number last()
The last function returns a number equal to the context size from the expression evaluation context. number position() The position function returns a number equal to the context position from the expression evaluation context. number count(node-set) The count function returns the number of nodes in the argument node-set. …. XPath [ Dr. M’hamed MATAOUI ]
23
XPath with JAVA XPath Steps for using XPath in JAVA
Following are the steps used while parsing a document using XPath Parser. Import XML-related packages. Create a DocumentBuilder Create a Document from a file or stream Create an Xpath object and an XPath path expression Compile the XPath expression using XPath.compile() and get a list of nodes by evaluating the compiled expression via XPath.evaluate() Iterate over the list of nodes. Examine attributes Examine sub-elements XPath [ Dr. M’hamed MATAOUI ]
24
XPath with JAVA XPath Example from : See also :
See also : Import XML-related packages import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import javax.xml.xpath.*; import java.io.*; Create a DocumentBuilder DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new File("xml_filename.xml")); XPath [ Dr. M’hamed MATAOUI ]
25
XPath with JAVA XPath Build XPath
XPath xPath = XPathFactory.newInstance().newXPath(); // Prepare Path expression and evaluate it String expression = "/course/plan/chapter"; NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET); // Iterate over NodeList for (int i = 0; i < nodeList.getLength(); i++) { Node nNode = nodeList.item(i); // // } XPath [ Dr. M’hamed MATAOUI ]
26
XPath with JAVA XPath Examine attributes //returns specific attribute
getAttribute("attributeName"); //returns a Map (table) of names/values getAttributes(); Examine sub-elements //returns a list of subelements of specified name getElementsByTagName("subelementName"); //returns a list of all child nodes getChildNodes(); XPath [ Dr. M’hamed MATAOUI ]
27
Xpath versions The main new features in XPath 3.1 are: 1. Maps
XPath 3 is the latest version of the XML Path Language, a query language for selecting nodes in XML documents. It supersedes XPath 1.0 and XPath 2.0. XPath 3.0 became a W3C Recommendation on 8 April 2014, while XPath 3.1 became a W3C Candidate Recommendation on 17 December 2015. The main new features in XPath 3.1 are: 1. Maps 2. Arrays From : Wikipedia XPath [ Dr. M’hamed MATAOUI ]
28
XQuery XQuery FLWOR Expressions
FLWOR (pronounced "flower") is an acronym for "For, Let, Where, Order by, Return". For - selects a sequence of nodes Let - binds a sequence to a variable Where - filters the nodes Order by - sorts the nodes Return - what to return (gets evaluated once for every node) From : XQuery [ Dr. M’hamed MATAOUI ]
29
XQuery XQuery FLWOR Expressions (Example) XQuery
In plain English, this could be read as "Get all departments that have more than ten employees, order these departments by decreasing average salary, and return a report of department numbers, head counts and average salary in each department" From : Wikipedia XQuery [ Dr. M’hamed MATAOUI ]
30
XQuery XQuery FLWOR Expressions (Demo example) XQuery
[ Dr. M’hamed MATAOUI ]
31
Adressing and querying XML documents
End of this Chapter For more details please visit: XPath and XQuery [ Dr. M’hamed MATAOUI ]
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.