450 likes | 771 Views
İ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.
E N D
İ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. • XPath provides to select nodes and compute values from the content of an XML document.
More Detailed Description • XPath is a syntax for defining parts of an XML document. • XPathusespathexpressionstonavigate in XML document. - Thispathexpressionsprovidestoselectnodesandnodesets in that file.
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.
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.
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.
AtomicValues Atomicvaluesarenodeswith no parentand no children. • Items Itemsareatomicvaluesornodes
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
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.
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
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
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
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
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
Mehmet Alparslan BAL XPATH
CONTENT • XPathAxes • XPathOperations • XPathFunctions • XPathUsage in XSLT
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.
XPath-Axes • Ancestor Contextnode is H ancestor::* • Ancestor-or-self Contextnode is H ancestor-or-self::*
XPath-Axes • Child Contextnode is B child::* • Parent Contextnode is J parent::*
XPath-Axes • Descendant Contextnode is A descendant::* • Descentant-or-self Contextnode is A descendant-or-self::*
XPath-Axes • Following Contextnode is B following::* • Following-sibling Contextnode is B following-sibling::*
XPath-Axes • Preceding Contextnode is C preceding::* • Preceding-sibling Contextnode is C preceding-sibling::*
XPath-Axes • Self Contextnode is C self::*
Xpath-Functions Node Set Functions StringFunctions BooleanFunctions NumberFunctions
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.
Xpath-Functions Example: /A/B[last()] • Selectthelast B child of element A. count(//B) • Returns 3. XML: <A> <B/> <B/> <B/> </A>
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.
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".
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.
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"/>
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.
Xpath-Functions Examples: ceiling(2.5) = 3 floor(3.5) = 3 number(‘3.5') = 3.5 round(2.6) = 3
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"/>
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>
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>
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>