1 / 41

XPATH

İnan AYKURT. XPATH . CONTENT . Introduction to XPATH XPATH Terminology XPATH Syntax. Introduction to XPATH. Brief definition: XPath is a language for finding information in an XML document It is used to navigate through elements and attributes in XML document.

eliot
Download Presentation

XPATH

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. İnan AYKURT XPATH

  2. CONTENT • Introduction to XPATH • XPATH Terminology • XPATH Syntax

  3. Introduction to XPATH • Brief definition: XPath is a language for finding information in an XML document • It is used to navigate through elements and attributes in XML document. • XPath provides to select nodes and compute values from the content of an XML document.

  4. More Detailed Description • XPath is a syntax for defining parts of an XML document. • XPathusespathexpressionstonavigate in XML document. - Thispathexpressionsprovidestoselectnodesandnodesets in that file.

  5. More Detailed Description • XPathcontains a library of standart functions. - XPath includes over 100 built-in functions. • XPath is a major element in the XSLT standard - XSLT (eXtensibleStylesheetLanguage Transformations) is the recommended style sheet language of XML.

  6. More Detailed Description • XPath is a W3C recommendation. - XPath became a W3C Recommendation in 1999. It was designed to be used by XSLT, XPointer and other XML parsing software. • Versioninformation - Thecurrentversion is secondversion. Itwasreleased as XPath 2.0 in 2007.

  7. XPath Terminology • Nodes There are seven kinds of nodes: -Element - Namespace - Attribute - Processing-instruction - Text - Comment - Document Nodes • XML documents behaves like trees of nodes. The topmost element of the tree is root element.

  8. AtomicValues Atomicvaluesarenodeswith no parentand no children. • Items Itemsareatomicvaluesornodes

  9. Example XML file:

  10. Some of nodes in theexample XML file: <CATALOG> (root element node)<ARTIST>Bob Dylan</ARTIST> (element node)lang="en" (attributenode) <YEAR>1985</YEAR> (element node) • Some of atomicvalues: “en” UK Bonnie Taylor

  11. Relationship of Nodes Tree of Nodes in an XML file *Nodes that have the same parentaresiblings. * Each element and attribute has one parent. * Element nodes may have zero, one or morechildren * A node's parent, parent's parent, etc.areancestors * A node's children, children's children, etc.aredescendants.

  12. XPathSyntax: SelectingNodes • Path expressions are used to select nodes or node-sets in XPath. - nodename : Selects all child nodes of the ‘nodename’ ex: CD, CATALOG - / : Selects from the root node ex: /CATALOG (select root element of CATALOG) CATALOG/CD (all CD elements that’re children of CATALOG) - // : Selects nodes in the document from the current node that match the selection no matter where they’re. ex: CATALOG//CD (selects allCD elements that are descendant of the CATALOG element) - . : Selects the current node - .. : Selects the parent of the current node - @ : Selectsattributes. ex: //@lang: Allattributesthatarenamedlang

  13. XPathSyntax: Predicates • Predicates are used to find a specific node or a node that contains a specific value. • Predicates are always embedded in square brackets. ex: /CATALOG/CD[last()] : Selects the last cd element that is the child of the CATALOG element /CATALOG/TITLE[last()-1] : Selects the last but one cdelement that is the child of the CATALOG element /CATALOG/TITLE[position()<3] :Selects the first two cd elements that are children of the CATALOG element //TITLE[@lang] :Selects all the title elements that have an attribute named lang with a value of 'eng' /CATALOG/CD[PRICE>35.00] :Selects all the cd elements of the CATALOG element that have a price element with a value greater than 35.00

  14. XPathSyntax: SelectingUnkownNodes • XPath wildcards can be used to select unknown XML elements • * : Matches any element node • @* : Matches any attribute node • node() : Matches any node of any kind of elements ex: /CATALOG/* : Selects all the child nodes of the CATALOG element //* : Selects all elements in the document //TITLE[@*] :Selects all title elements which have any attribute

  15. XPathSyntax: SelectingSeveralPaths • By using the | operator in an XPath expression you can select several paths. • Inotherwords | means AND in XPathsyntax. ex: //CD/TITLE | //CD/PRICE : Alltitles of thecd’s AND allprices of cd’s. //TITLE | //PRICE : Alltitleandpriceelements in the XML file /CATALOG/CD/TITLE | //PRICE : Alltitles of thecd element of thecatalogandallpriceelements in the file

  16. Example

  17. SomeExampleQueries Path Expression Result /bookstore/book[price>35]/title XQuery Kick Start Learning XML /bookstore/book[year = 2005]/authorGiada De Laurentiis J. K. Rowling //title[@category = ‘COOKING’] EverydayItalian //title[@*]EverydayItalian Harry Potter Xquery Kick Start Learning XML /bookstore/book/title[@category= ‘WEB’] | //price XQuery Kick Start Learning XML 30 29.99 49.99 39.95

  18. Mehmet Alparslan BAL XPATH

  19. CONTENT • XPathAxes • XPathOperations • XPathFunctions • XPathUsage in XSLT

  20. XPath-Axes • An axis stores certain information about the context node or other nodes within the document. The information it stores depends on the axis being used. • An axis defines a node-set relative to the current node.

  21. XPath-Axes

  22. XPath-Axes • Ancestor Contextnode is H ancestor::* • Ancestor-or-self Contextnode is H ancestor-or-self::*

  23. XPath-Axes • Child Contextnode is B child::* • Parent Contextnode is J parent::*

  24. XPath-Axes • Descendant Contextnode is A descendant::* • Descentant-or-self Contextnode is A descendant-or-self::*

  25. XPath-Axes • Following Contextnode is B following::* • Following-sibling Contextnode is B following-sibling::*

  26. XPath-Axes • Preceding Contextnode is C preceding::* • Preceding-sibling Contextnode is C preceding-sibling::*

  27. XPath-Axes • Self Contextnode is C self::*

  28. Xpath-Operations

  29. Xpath-Functions Node Set Functions StringFunctions BooleanFunctions NumberFunctions

  30. Xpath-Functions Node Set Functions: - Takes a node-set argument, returns a node-set, or returns/provides information about a particular node within a node-set. last() : Returns the last node in a node set. count(node-set) : Returns the number of nodes matching the node-set pattern. local-part(node) : Returns the local part of the node's name. In other words, the node name stripped of the namespace prefix. name(node) : Returns the node's name. id(idref) : Returns the node with the 'ID' attribute equals to idref. The ID attribute is assigned in the DTD. The actual attribute name may, of course, vary.

  31. Xpath-Functions Example: /A/B[last()] • Selectthelast B child of element A. count(//B) • Returns 3. XML: <A> <B/> <B/> <B/> </A>

  32. Xpath-Functions StringFunctions: - Performs evaluations, formatting, and manipulation on string arguments.. string(obj) : Converts the object to a string. concat(s1, s2, ...) : Concatenates strings. starts-with(string, head) : True if string starts with head. contains(string, substring) : True if string contains substring. substring (string, pos, len) : Returns the substring of string starting at pos andthelength of thesubstring is len. substring-before(string, substring) : Returns the substring of string before matching substring. substring-after(string, substring) : Returns the substring of string after the matching substring. string-length(string) : Returns the string length of string. normalize-space(string) : Normalizes whitespace. Consecutive whitespace is converted into a single space. Leading and trailing whitespace is trimmed. translate(string, from, to) : Converts characters in a string, like the 'tr' command.

  33. Xpath-Functions Examples: concat("abc", "d", "ef", "g")returns "abcdefg". string-length("abcdefg") returns 7. substring(“abcdefg",2,3) returns "bcd ". substring-after(" abcdefg ", "cd") returns " efg". substring-before(" abcdefg ", "cd") returns "ab". translate("abcdefg","abc","ABC") returns "ABCdefg". translate("abcdefg","abcd","ABC") returns "ABCefg".

  34. Xpath-Functions BooleanFunctions: - Evaluates the argument expressions to obtain a Boolean result. boolean(obj) : Converts obj into a boolean. true() : Returns true. false() : Returns false. lang(language) : Returns true if the context node has an xml:lang of language.

  35. Xpath-Functions Example: - The following function call returns true for all: lang("en") XML <para xml:lang="en"/> <divxml:lang="en"><para/></div> <para xml:lang="EN"/> <para xml:lang="en-us"/>

  36. Xpath-Functions NumberFunctions: - Evaluates the argument expressions to obtain a numeric result. number(obj) : Converts obj to a number . sum(node-set) : Converts the node values of node-set to numbers and adds them. floor(number) : Returns the closest integer below number. ceiling(number) : Returns the closest integer above number. round(number) : Rounds number to the nearest integer.

  37. Xpath-Functions Examples: ceiling(2.5) = 3 floor(3.5) = 3 number(‘3.5') = 3.5 round(2.6) = 3

  38. XPathUsage in XSLT In XSLT, XPath is usedtomatchnodesfromthesourcedocumentforoutputtemplatesviathematchattribute of thexsl:templatetag. <xsl:templatematch="SOME_XPATH"> XPath is alsoused in thexsl:value-of tagtospecifyelements, attributesandtexttodecidewhattooutput. <xsl:value-of select="SOME_XPATH"/>

  39. XPathUsage in XSLT Example: XML document <?xmlversion="1.0" ?> <persons> <personusername="JS1"> <name>John</name> <family-name>Smith</family-name> </person> <personusername="MI1"> <name>Morka</name> <family-name>Ismincius</family-name> </person> </persons>

  40. XPathUsage in XSLT This XSLT stylesheetprovidestemplatestotransformthe XML document: <?xmlversion="1.0" encoding="UTF-8"?> <xsl:stylesheetxmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:outputmethod="xml" indent="yes"/> <xsl:templatematch="/persons"> <root> <xsl:apply-templatesselect="person"/> </root> </xsl:template> <xsl:templatematch="person"> <name username="{@username}"> <xsl:value-of select="name" /> </name> </xsl:template> </xsl:stylesheet>

  41. XPathUsage in XSLT Itsevaluationresults in a new XML document, havinganotherstructure: <?xmlversion="1.0" encoding="UTF-8"?> <root> <name username="JS1">John</name> <name username="MI1">Morka</name> </root>

More Related