Contents n XPath n XQuery XPath n n

  • Slides: 35
Download presentation

Contents n XPath n XQuery

Contents n XPath n XQuery

XPath n n n Syntax for defining parts of an XML document Uses paths

XPath n n n Syntax for defining parts of an XML document Uses paths to define XML elements Defines a library of standard functions Major element in XSLT Not written in XML W 3 C Standard

XPath - Introduction <? xml version="1. 0" encoding="ISO-8859 -1"? > <catalog> <cd country="USA"> <title>Empire

XPath - Introduction <? xml version="1. 0" encoding="ISO-8859 -1"? > <catalog> <cd country="USA"> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <price>10. 90</price> </cd> <cd country="UK"> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <price>9. 90</price> </cd> <cd country="USA"> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <price>9. 90</price> </cd> </catalog> n Like Traditional File Paths n n XPath Defines a Library of Standard Functions n n /catalog/cd[price>10. 80] XPath is Used in XSLT n n /catalog/cd/price XPath is a major element of the XSLT standard. XPath is a W 3 C Standard n XPath was released as a W 3 C Recommendation 16. November 1999.

XPath Syntax <? xml version="1. 0" encoding="ISO-8859 -1"? > <catalog> <cd country="USA"> <title>Empire Burlesque</title>

XPath Syntax <? xml version="1. 0" encoding="ISO-8859 -1"? > <catalog> <cd country="USA"> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <price>10. 90</price> </cd> <cd country="UK"> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <price>9. 90</price> </cd> <cd country="USA"> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <price>9. 90</price> </cd> </catalog> n Locating Nodes n n n Selecting Unknown Elements n n n n /catalog/cd[1] /catalog/cd[last()] /catalog/cd[price=10. 90]/price Selecting Several Paths n n n /catalog/cd/* /catalog/*/price /*/*/price //* Selecting Branches n n /catalog/cd/price //cd /catalog/cd/title | /catalog/cd/artist /catalog/cd/title | //artist Selecting Attributes n n //@country //cd[@country] //cd[@*] //cd[@country='UK']

XPath Location Paths Location Path Expression n n n An absolute location path: /step/.

XPath Location Paths Location Path Expression n n n An absolute location path: /step/. . . A relative location path: step/. . . axisname: : nodetest[predicate] n n n axisname nodetest predicate

XPath Location Paths Axes and Node Tests - 1 n Axes and Node Tests

XPath Location Paths Axes and Node Tests - 1 n Axes and Node Tests Axis. Name Description ancestor Contains all ancestors (parent, grandparent, etc. ) of the current node Note: This axis will always include the root node, unless the current node is the root node ancestor-or-self Contains the current node plus all its ancestors (parent, grandparent, etc. ) attribute Contains all attributes of the current node child Contains all children of the current node descendant Contains all descendants (children, grandchildren, etc. ) of the current node Note: This axis never contains attribute or namespace nodes descendant-or-self Contains the current node plus all its descendants (children, grandchildren, etc. ) following Contains everything in the document after the closing tag of the current node following-sibling Contains all siblings after the current node Note: If the current node is an attribute node or namespace node, this axis will be empty namespace Contains all namespace nodes of the current node parent Contains the parent of the current node preceding Contains everything in the document that is before the starting tag of the current node preceding-sibling Contains all siblings before the current node Note: If the current node is an attribute node or namespace node, this axis will be empty self Contains the current node

XPath Location Paths Axes and Node Tests - 2 n Axes and Node Tests

XPath Location Paths Axes and Node Tests - 2 n Axes and Node Tests Example Result child: : cd Selects all cd elements that are children of the current node (if the current node has no cd children, it will select an empty node-set) attribute: : src Selects the src attribute of the current node (if the current node has no src attribute, it will select an empty node-set) child: : * Selects all child elements of the current node attribute: : * Selects all attributes of the current node child: : text() Selects the text node children of the current node child: : node() Selects all the children of the current node descendant: : cd Selects all the cd element descendants of the current node ancestor: : cd Selects all cd ancestors of the current node ancestor-or-self: : cd Selects all cd ancestors of the current node and, if the current node is a cd element, the current node as well child: : */child: : price Selects all price grandchildren of the current node / Selects the document root

XPath Location Paths Predicates n A predicate filters a node-set into a new node-set.

XPath Location Paths Predicates n A predicate filters a node-set into a new node-set. Example Result child: : price[price=9. 90] Selects all price elements that are children of the current node with a price element that equals 9. 90 child: : cd[position()=1] Selects the first cd child of the current node child: : cd[position()=last()] Selects the last cd child of the current node child: : cd[position()=last()-1] Selects the last but one cd child of the current node child: : cd[position()<6] Selects the first five cd children of the current node /descendant: : cd[position()=7] Selects the seventh cd element in the document child: : cd[attribute: : type="classic"] Selects all cd children of the current node that have a type attribute with value classic

XPath Location Paths Location Path Abbreviated Syntax - 1 n Abbreviations can be used

XPath Location Paths Location Path Abbreviated Syntax - 1 n Abbreviations can be used when describing a location path. Abbr Meaning Example none child: : cd is short for child: : cd @ attribute: : cd[@type="classic"] is short for child: : cd[attribute: : type="classic"] . self: : node() . //cd is short for self: : node()/descendant-orself: : node()/child: : cd . . parent: : node() . . /cd is short for parent: : node()/child: : cd // /descendant-or-self: : node()/ //cd is short for /descendant-or-self: : node()/child: : cd

XPath Location Paths Location Path Abbreviated Syntax - 2 Example Result cd Selects all

XPath Location Paths Location Path Abbreviated Syntax - 2 Example Result cd Selects all the cd elements that are children of the current node * Selects all child elements of the current node text() Selects all text node children of the current node @src Selects the src attribute of the current node @* Selects all the attributes of the current node cd[1] Selects the first cd child of the current node cd[last()] Selects the last cd child of the current node */cd Selects all cd grandchildren of the current node /book/chapter[3]/para[1] Selects the first para of the third chapter of the book //cd Selects all the cd descendants of the document root and thus selects all cd elements in the same document as the current node . Selects the current node . //cd Selects the cd element descendants of the current node . . Selects the parent of the current node . . /@src Selects the src attribute of the parent of the current node cd[@type="classic"] Selects all cd children of the current node that have a type attribute with value classic cd[@type="classic"][5] Selects the fifth cd child of the current node that has a type attribute with value classic cd[5][@type="classic"] Selects the fifth cd child of the current node if that child has a type attribute with value classic cd[@type and @country] Selects all the cd children of the current node that have both a type

XPath Expressions - 1 n XPath supports numerical, equality, relational, and Boolean expressions. n

XPath Expressions - 1 n XPath supports numerical, equality, relational, and Boolean expressions. n Numerical Expressions Operator Description Example Result + Addition 6+4 10 - Subtraction 6 -4 2 * Multiplication 6*4 24 div Division 8 div 4 2 mod Modulus (division remainder) 5 mod 2 1

XPath Expressions - 2 n n Equality Expressions Operator Description Example Result = Like

XPath Expressions - 2 n n Equality Expressions Operator Description Example Result = Like (equal) price=9. 80 true (if price is 9. 80) != Not like (not equal) price!=9. 80 false Testing Against a Node-Set n n n If the test value is tested for equality against a node-set, the result is true if the node-set contains any node with a value that matches the test value. If the test value is tested for not equal against a nodeset, the result is true if the node-set contains any node with a value that is different from the test value. The result is that the node-set can be equal and not equal at the same time !!!

XPath Expressions - 3 n n Relational Expressions Operator Description Example Result < Less

XPath Expressions - 3 n n Relational Expressions Operator Description Example Result < Less than price<9. 80 false (if price is 9. 80) <= Less or equal price<=9. 80 true > Greater than price>9. 80 false >= Greater or equal price>=9. 80 true Boolean Expressions Operator Description Example Result or or price=9. 80 or price=9. 70 true (if price is 9. 80) and price<=9. 80 and price=9. 70 false

XPath Function Library - 1 n The XPath function library contains a set of

XPath Function Library - 1 n The XPath function library contains a set of core functions for converting and translating data. n Node Set Functions Description Syntax count() Returns the number of nodes in a node-set number=count(node-set) id() Selects elements by their unique ID node-set=id(value) last() Returns the position number of the last node in the processed node list number=last() local-name() Returns the local part of a node. A node usually consists of a prefix, a colon, followed by the local name string=local-name(node) name() Returns the name of a node string=name(node) namespaceuri() Returns the namespace URI of a specified node uri=namespace-uri(node) position() Returns the position in the node list of the node that is currently being processed number=position()

XPath Function Library - 2 n String Functions Name Description Syntax & Example concat()

XPath Function Library - 2 n String Functions Name Description Syntax & Example concat() Returns the concatenation of all its arguments string=concat(val 1, val 2, . . ) Example: concat('The', 'XML') contains() Returns true if the second string is contained within the first string, otherwise it returns false bool=contains(val, substr) Example: contains('XML', 'X') normalizespace() Removes leading and trailing spaces from a string, and replaces all internal sequences of white with one white space string=normalize-space(string) Example: normalize-space(' The XML ') starts-with() Returns true if the first string starts with the second string, otherwise it returns false bool=starts-with(string, substr) Example: starts-with('XML', 'X') Result: true string() Converts the value argument to a string(value) Example: string(314) stringlength() Returns the number of characters in a string number=string-length(string) Example: string-length('Beatles') substring() Returns a part of the string in the string argument string=substring(string, start, length) Example: substring('Beatles', 1, 4) Result: 'Beat' substringafter() Returns the part of the string in the string argument that occurs after the substring in the substr argument string=substring-after(string, substr) Example: substring-after('12/10', '/') substringbefore() Returns the part of the string in the string argument that occurs before the substring in the substr argument string=substring-before(string, substr) Example: substring-before('12/10', '/') translate() Performs a character by character replacement. It looks in the value argument for characters contained in string 1, and replaces each character for the one in the same position in the string 2 string=translate(value, string 1, string 2) Examples: translate('12: 30', '45') Result: '12: 45' translate('12: 30', '03', '54') Result: '12: 45' translate('12: 30', '0123', 'abcd') Result: 'bc: da' Result: 'The XML' Result: true Result: 'The XML' Result: '314' Result: 7 Result: '10' Result: '12'

XPath Function Library - 3 n n Number Functions Name Description Syntax & Example

XPath Function Library - 3 n n Number Functions Name Description Syntax & Example ceiling() Returns the smallest integer that is not less than the number argument number=ceiling(number) Example: ceiling(3. 14) floor() Returns the largest integer that is not greater than the number argument number=floor(number) Example: floor(3. 14) number() Converts the value argument to a number=number(value) Example: number('100') round() Rounds the number argument to the nearest integer=round(number) Example: round(3. 14) sum() Returns the total value of a set of numeric values in a node-set number=sum(nodeset) Example: sum(/cd/price) Result: 4 Result: 3 Result: 100 Result: 3 Boolean Functions Name Description Syntax & Example boolean() Converts the value argument to Boolean and returns true or false bool=boolean(value) false() Returns false() Example: number(false()) lang() Returns true if the language argument matches the language of the xsl: lang element, otherwise it returns false bool=lang(language) not() Returns true if the condition argument is false, and false if the condition argument is true bool=not(condition) Example: not(false()) true() Returns true() Example: number(true()) Result: 0 Result: 1

XPath - Example <? xml version="1. 0" encoding="ISO-8859 -1"? > n <catalog> Selecting cd

XPath - Example <? xml version="1. 0" encoding="ISO-8859 -1"? > n <catalog> Selecting cd Nodes n xml. Doc. select. Nodes("/catalog/cd") <cd> <title>Empire Burlesque</title> n <artist>Bob Dylan</artist> n <country>USA</country> <company>Columbia</company> n </cd> n xml. Doc. select. Nodes("/catalog/cd[0]") Selecting price Nodes n <price>10. 90</price> <year>1985</year> Selecting the First cd Node xml. Doc. select. Nodes("/catalog/cd/price") Selecting price Text Nodes n xml. Doc. select. Nodes("/catalog/cd/price/text()") <cd> <title>Hide your heart</title> n <artist>Bonnie Tyler</artist> Selecting cd Nodes with Price>10. 80 n xml. Doc. select. Nodes("/catalog/cd[price>10. 80]") <country>UK</country> <company>CBS Records</company> <price>9. 90</price> <year>1988</year> </cd> </catalog> n Selecting price Nodes with Price>10. 80 n xml. Doc. select. Nodes("/catalog/cd[price>10. 80]/price")

XPath – Example with Java n Package javax. xml. xpath , Since 1. 5

XPath – Example with Java n Package javax. xml. xpath , Since 1. 5 <widgets> <widget> <manufacturer/> Document. Builder builder = Document. Builder. Factory. new. Instance(). new. Document. Builder(); Document document = builder. parse(new File("/widgets. xml")); XPath xpath = XPath. Factory. new. Instance(). new. XPath(); String expression = "/widgets/widget"; Node widget. Node = (Node) xpath. evaluate(expression, document, XPath. Constants. NODE); <dimensions/> </widgets> XPath xpath = XPath. Factory. new. Instance(). new. XPath(); String expression = "manufacturer"; Node manufacturer. Node = (Node) xpath. evaluate(expression, widget. Node, XPath. Constants. NODE);

XQuery n n n Language for querying XML data Built on XPath expressions XQuery

XQuery n n n Language for querying XML data Built on XPath expressions XQuery for XML is like SQL for databases

XQuery - Introduction n XQuery is About Querying XML n n XQuery and XPath

XQuery - Introduction n XQuery is About Querying XML n n XQuery and XPath n n XQuery is a language for finding and extracting (querying) data from XML documents. XQuery 1. 0 and XPath 2. 0 shares the same data model, the same functions, and the same syntax. XQuery is Not (yet) a Web Standard n XQuery 1. 0 is still a W 3 C Working Draft.

Xquery - FLWOR n For , Let , Where , Order by , Return

Xquery - FLWOR n For , Let , Where , Order by , Return <? xml version="1. 0" encoding="ISO-8859 -1"? > <bib> <book year="1994"> <title>TCP/IP Illustrated</title> <author><last>Stevens</last><first>W. </first></author> <price>65. 95</price> </book> <book year="1992"> <title>Advanced Programming in the Unix environment</title> <author><last>Stevens</last><first>W. </first></author> <price>65. 95</price> </book> <book year="2000"> <title>Data on the Web</title> <author><last>Abiteboul</last><first>Serge</first></author> <author><last>Buneman</last><first>Peter</first></author> <author><last>Suciu</last><first>Dan</first></author> <price>39. 95</price> </book> <book year="1999"> <title>The Technology and Content for Digital TV</title> <editor> <last>Gerbarg</last><first>Darcy</first> <affiliation>CITI</affiliation> </editor> <price>129. 95</price> </book> </bib> for $x in doc("books. xml")/bib/book where $x/price>50 order by $x/title return $x/title <title>Advanced Programming in the Unix environment</title> <title>TCP/IP Illustrated</title> <title>The Technology and Content for Digital TV</title>

XQuery Reference n n n XQuery Locations XQuery Operators XQuery Functions n n same

XQuery Reference n n n XQuery Locations XQuery Operators XQuery Functions n n same data model as XPath 2. 0 XQuery Data Types n same data types as XSD (XML Schema 1. 0) n n XSD XSD String Date Numeric Misc

XQuery Data Types XSD String n String Data Type n n n Normalized. String

XQuery Data Types XSD String n String Data Type n n n Normalized. String Data Type n n n can contain characters, line feeds, carriage returns, and tab characters. <xs: element name="customer" type="xs: string"/> XML processor will remove line feeds, carriage returns, and tab characters. <xs: element name="customer" type="xs: normalized. String"/> Token Data Type n n the XML processor will remove line feeds, carriage returns, tabs, leading and trailing spaces, and multiple spaces. <xs: element name="customer" type="xs: token"/>

XQuery Data Types XSD String n String Data Types n <xs: element name="customer" type="xs:

XQuery Data Types XSD String n String Data Types n <xs: element name="customer" type="xs: normalized. String"/> Name Description ENTITIES ENTITY ID A string that represents the ID attribute in XML (only used with schema attributes) IDREF A string that represents the IDREF attribute in XML (only used with schema attributes) IDREFS language A string that contains a valid language id Name A string that contains a valid XML name NCName NMTOKEN A string that represents the NMTOKEN attribute in XML (only used with schema attributes) NMTOKENS normalized. String A string that does not contain line feeds, carriage returns, or tabs QName string A string token A string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces

XQuery Data Types XSD Date and Time Data Types n Date Data Type n

XQuery Data Types XSD Date and Time Data Types n Date Data Type n n The date is specified in the following form "YYYY-MM-DD" <xs: element name="start" type="xs: date"/> <start>2002 -09 -24</start> Time Zones n n <start>2002 -09 -24 Z</start> <start>2002 -09 -24 -06: 00</start> or <start>2002 -09 -24+06: 00</start>

XQuery Data Types XSD Date and Time Data Types n Time Data Type n

XQuery Data Types XSD Date and Time Data Types n Time Data Type n n The time is specified in the following form "hh: mm: ss" <xs: element name="start" type="xs: time"/> <start>09: 00</start> or <start>09: 30: 10. 5</start> Time Zones n n <start>09: 30: 10 Z</start> <start>09: 30: 10 -06: 00</start> or <start>09: 30: 10+06: 00</start>

XQuery Data Types XSD Date and Time Data Types n Date. Time Data Type

XQuery Data Types XSD Date and Time Data Types n Date. Time Data Type n n The date. Time is specified in the following form "YYYYMM-DDThh: mm: ss" <xs: element name="startdate" type="xs: date. Time"/> <startdate>2002 -05 -30 T 09: 00</startdate> or <startdate>2002 -05 -30 T 09: 30: 10. 5</startdate> Time Zones n n <startdate>2002 -05 -30 T 09: 30: 10 Z</startdate> <startdate>2002 -05 -30 T 09: 30: 10 -06: 00</startdate> or <startdate>2002 -05 -30 T 09: 30: 10+06: 00</startdate>

XQuery Data Types XSD Date and Time Data Types n Duration Data Type n

XQuery Data Types XSD Date and Time Data Types n Duration Data Type n n n The time interval is specified in the following form "Pn. Yn. Mn. DTn. Hn. Mn. S" <xs: element name="period" type="xs: duration"/> <period>P 5 Y</period> or <period>P 5 Y 2 M 10 DT 15 H</period> or <period>PT 15 H</period> or <period>-P 10 D</period>

XQuery Data Types XSD Date and Time Data Types n <xs: element name="startdate" type="xs:

XQuery Data Types XSD Date and Time Data Types n <xs: element name="startdate" type="xs: date. Time"/> Name Description date Defines a date value date. Time Defines a date and time value duration Defines a time interval g. Day Defines a part of a date - the day (DD) g. Month Defines a part of a date - the month (MM) g. Month. Day Defines a part of a date - the month and day (MM-DD) g. Year Defines a part of a date - the year (YYYY) g. Year. Month Defines a part of a date - the year and month (YYYY-MM) time Defines a time value

XQuery Data Types – XSD Numeric Data Types n Decimal Data Type ( maximum

XQuery Data Types – XSD Numeric Data Types n Decimal Data Type ( maximum 18! ) n n n <xs: element name="prize" type="xs: decimal"/> <prize>999. 50</prize> or <prize>+999. 5450</prize> or <prize>-999. 5230</prize> or <prize>0</prize> Integer Data Type n n <xs: element name="prize" type="xs: integer"/> <prize>999</prize> or <prize>+999</prize> or <prize>-999</prize> or <prize>0</prize>

XQuery Data Types – XSD Numeric Data Types n <xs: element name="prize" type="xs: integer"/>

XQuery Data Types – XSD Numeric Data Types n <xs: element name="prize" type="xs: integer"/> Name Description byte A signed 8 -bit integer decimal A decimal value int A signed 32 -bit integer An integer value long A signed 64 -bit integer negative. Integer An integer containing only negative values (. . , -2, -1. ) non. Negative. Integer An integer containing only non-negative values (0, 1, 2, . . ) non. Positive. Integer An integer containing only non-positive values (. . , -2, -1, 0) positive. Integer An integer containing only positive values (1, 2, . . ) short A signed 16 -bit integer unsigned. Long An unsigned 64 -bit integer unsigned. Int An unsigned 32 -bit integer unsigned. Short An unsigned 16 -bit integer unsigned. Byte An unsigned 8 -bit integer

XQuery Data Types – XSD Miscellaneous Data Types n Boolean Data Type n n

XQuery Data Types – XSD Miscellaneous Data Types n Boolean Data Type n n <xs: attribute name="disabled" type="xs: boolean"/> <prize disabled="true">999</prize> Legal values for boolean are true, false, 1 (which indicates true), and 0 (which indicates false). Binary Data Types n Binary data types are used to express binary-formatted data. n n base 64 Binary (Base 64 -encoded binary data) hex. Binary (hexadecimal-encoded binary data) <xs: element name="blobsrc" type="xs: hex. Binary"/> Any. URI Data Type n n The any. URI data type is used to specify a URI <xs: attribute name="src" type="xs: any. URI"/> <pic src="http: //www. w 3 schools. com/images/smiley. gif" /> If a URI has spaces, replace them with %20.

XQuery Data Types – XSD Miscellaneous Data Types n <xs: attribute name="disabled" type="xs: boolean"/>

XQuery Data Types – XSD Miscellaneous Data Types n <xs: attribute name="disabled" type="xs: boolean"/> Name any. URI base 64 Binary boolean double float hex. Binary NOTATION QName

XQuery – Example with Java n JSR 225: XQuery API for Java. TM (XQJ)

XQuery – Example with Java n JSR 225: XQuery API for Java. TM (XQJ) XQData. Source data. Source =. . . ; XQConnection con = data. Source. get. Connection("my. UID", "my. PWD"); // Find all Employees belong to the SW department from the EMPS collection String qs = "for $e in collection('EMPS. xml') where $e/Dept='SW' return $e"; XQPrepared. Expression = con. prepared. Expression(qs); XQResult. Sequence rs = expression. execute. Query(); // JAXBHandler class is provided by the application or by a service provider // Creates a JAXB handler for com. acme. xml package JAXBContext jaxb. Context = JAXBContext. new. Instance("com. acme. xml"); XQCommon. Handler jaxb. Handler = new JAXBHandler(jaxb. Context); while(rs. next()) { // Employee is a JAXB object defined in com. acme. xml package // Note that we could have set the jaxb. Handler at the connection // level, before preparing the expression, in which case, // we need not supply the handler to the get. Object. Employee emp = (Employee) rs. get. Object(jaxb. Handler); // use the Employee object assuming Employee implements to. String() System. out. println(emp); }