Presentation is loading. Please wait.

Presentation is loading. Please wait.

CITA 330 Section 5 XPath. XSL XSL (Extensible Stylesheet Language) is the standard language for writing stylesheets to transform XML documents among different.

Similar presentations


Presentation on theme: "CITA 330 Section 5 XPath. XSL XSL (Extensible Stylesheet Language) is the standard language for writing stylesheets to transform XML documents among different."— Presentation transcript:

1 CITA 330 Section 5 XPath

2 XSL XSL (Extensible Stylesheet Language) is the standard language for writing stylesheets to transform XML documents among different dialects or into other languages XSL stylesheets are pure XML documents XSL includes three components: –XPath as a standard notation system for specifying subsets of elements in an XML document –XSLT (XSL Transformation) as an XML dialect for specifying XML transformation rules or stylesheets –XSL-FO for formatting XML documents

3 Example XML Document Gone with the Wind Movie Classic Star Trek TV Series Science fiction

4 Identifying XML Nodes with XPath Visualize all components in an XML document, including the elements, attributes and text, as graph nodes A node is connected to another node under it if the latter is immediately nested in the former or is an attribute or text value of the former The attribute names have symbol @ as their prefix The sibling nodes are ordered as they appear in the XML document library dvd title format genre @id dvd title format genre @id

5 Path Expressions Path expressions are used to select nodes in an XML document An absolute location path starts with a slash / and has the general form of /step/step/… A relative location path does not start with a slash / and has the general form of step/step/… In both cases, the path expression is evaluated from left to right, and each step is evaluated in the current node set to refine it

6 Path Expressions Each step has the following general form: [axisName::]nodeTest[predicate] –the optional axis name specifies the tree- relationship between the selected nodes and the current node –the node test identifies a node type within an axis –zero or more predicates are for further refining the selected node set

7 Path Expressions ExpressionDescription nodeNameSelects all child nodes of the named node /Selects from the root node //Selects nodes in the document from the current node that match the selection no matter where they are.Selects the current node..Selects the parent of the current node @Selects attributes text()Selects the text value of the current element *Selects any element nodes @*Selects any attribute node node()Selects any node of any kind (elements, attributes, …)

8 Path Expressions library: all the library elements in the current node set /library: the root element library library/dvd: all dvd elements that are children of library elements in the current node set //dvd: all dvd elements no matter where they are in the document (no matter how many levels they are nested in other elements) relative to the current node set library//title: all title elements that are descendants of the library elements in the current node set no matter where they are under the library elements //@id: all attributes that are named “id” relative to the current node set

9 Path Expressions /library/dvd/title/text(): the text values of all the title elements of the dvd elements /library/dvd[1]: the first dvd child element of library /library/dvd[last()]: the last dvd child element of library /library/dvd[last()-1]: the last but one dvd child element of library /library/dvd[position()<3]: the first two dvd child elements of library //dvd[@id]: all dvd elements that have an id attribute //dvd[@id='2']: the dvd element that has an id attribute with value 2

10 Path Expressions /library/dvd[genre='Classic']: all dvd child elements of library that have “Classic” as their genre value /library/dvd[genre='Classic']/title: all title elements of dvd elements of library that have “Classic” as their genre value /library/*: all the child nodes of the library element //*: all elements in the document //dvd[@*]: all dvd elements that have any attribute //title | //genre: all title and genre elements in the document

11 Popular Axis Names Axis NameResult ancestorSelects all ancestors (parent, grandparent, etc.) of the current node ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself attributeSelects all attributes of the current node childSelects all children of the current node descendantSelects all descendants (children, grandchildren, etc.) of the current node descendant-or-selfSelects all descendants (children, grandchildren, etc.) of the current node and the current node itself followingSelects everything in the document after the end tag of the current node following-siblingSelects all siblings after the current node namespaceSelects all namespace nodes of the current node parentSelects the parent of the current node precedingSelects everything in the document that is before the start tag of the current node preceding-siblingSelects all siblings before the current node selfSelects the current node

12 Path Expressions child::dvd: all dvd nodes that are children of the current node attribute::id: the id attribute of the current node child::*: all children of the current node attribute::*: all attributes of the current node child::text(): all text child nodes of the current node child::node(): all child nodes of the current node descendant::dvd: all dvd descendants of the current node ancestor::dvd: all dvd ancestors of the current node child::*/child::title: all title grandchildren of the current node

13 Arithmetic Expressions + add - subtract * multiply div (not / ) divide mod modulo (remainder)

14 Boolean Operators = means “equal to”(Notice it’s not == ) != means “not equal to” and (infix operator) or (infix operator) not() (function) The following are used for numerical comparisons only: < “less than” <= “less than or equal to” > “greater than” >= “greater than or equal to”

15 XPath Functions XPath contains a number of functions on node sets, numbers, and strings; here are a few of them: –count( elem ) counts the number of selected elements Example: //chapter[count(section)=2] selects chapter s with exactly two section children –name() returns the name of the element Example: //*[name()='section'] is the same as //section –starts-with( arg1, arg2 ) tests if arg1 starts with arg2 Example: //*[starts-with(name(), 'sec'] –contains( arg1, arg2 ) tests if arg1 contains arg2 Example: //*[contains(name(), 'ect')]

16 XPath Boolean Functions boolean() false() lang() not() true()

17 XPath Node-Set Functions count() id() last() local-name() name() namespace-uri() position()

18 XPath Numeric Functions ceiling() floor() number() round() sum()

19 XPath String Functions concat() contains() normalize-space() starts-with() string() string-length() substring() substring-after() substring-before() translate()


Download ppt "CITA 330 Section 5 XPath. XSL XSL (Extensible Stylesheet Language) is the standard language for writing stylesheets to transform XML documents among different."

Similar presentations


Ads by Google