Download presentation
Presentation is loading. Please wait.
1
XML Language Family Detailed Examples Most information contained in these slide comes from: http://www.w3.org, http://www.zvon.org/ These slides are intended to be used as a tutorial on XML and related technologies Slide author: Jürgen Mangler (juergen.mangler@univie.ac.at) This section contains examples on: XPath, XPointer
2
XPath is the result of an effort to provide a common syntax and semantics for functionality shared between XSL Transformations [XSLT] and XPointer. The primary purpose of XPath is to address parts of an XML document.[XSLT] XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document.
3
In addition to its use for addressing, XPath is also designed to feature a natural subset that can be used for matching (testing whether or not a node matches a pattern); this use of XPath is described in XSLT (next chapter).XSLT XPath models an XML document as a tree of nodes. There are different types of nodes, including element nodes, attribute nodes and text nodes.
4
The basic XPath syntax is similar to filesystem addressing. If the path starts with the slash /, then it represents an absolute path to the required element. /AAA/CCC Select all elements CCC which are children of the root element AAA /AAA Select the root element AAA
5
If the path starts with // then all elements in the document, that fulfill the criteria following //, are selected. //DDD/BBB Select all elements BBB which are children of DDD //BBB Select all elements BBB
6
The star * selects all elements located by the preceeding path /*/*/*/BBB Select all elements BBB which have 3 ancestors /AAA/CCC/DDD/* Select all elements enclosed by elements /AAA/CCC/DDD
7
The expression in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection. /papers/paper[last()] Select the last BBB child of element AAA /papers/paper[1] Select the first BBB child of element AAA
8
Attributes are specified by @ prefix. //student[@matnr] Select BBB elements which have attribute id //@matnr Select all attributes @matnr //student[not(@*)] Select BBB elements without an attribute //student[@*] Select BBB elements which have any attribute
9
Values of attributes can be used as selection criteria. Function normalize-space removes leading and trailing spaces and replaces sequences of whitespace characters by a single space. //student[normalize-space(@name)='hauer'] Select BBB elements which have an attribute name with value bbb, leading and trailing spaces are removed before comparison //student[@id='b1'] Select BBB elements which have attribute id with value b1
10
Function count() counts the number of selected elements //*[count(*)=3] Select elements which have 3 children //*[count(BBB)=2] Select elements which have two children BBB
11
Several paths can be combined with | separator ("|" stands for "or", like the logical or operator in C). /AAA/EEE | //DDD/CCC | /AAA | //BBB Number of combinations is not restricted AAA/EEE | //BBB Select all elements BBB and elements EEE which are children of root element AAA
12
Axes are a sophisticated concept in XML to find out which nodes relate to each other and how. The above example illustrates how axes work. Starting with node an axe would select the equal named nodes. This example is also the base for the next two pages. parent node following- sibling preceding- sibling descendant
13
The following main axes are available: the child axis contains the children of the context node the descendant axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace nodes the parent axis contains the parent of the context node, if there is oneparent the following-sibling axis contains all the following siblings of the context node; if the context node is an attribute node or namespace node, the following-sibling axis is empty the preceding-sibling axis contains all the preceding siblings of the context node; if the context node is an attribute node or namespace node, the preceding-sibling axis is empty (http://www.w3.org/TR/xpath#axes)
14
The child axis contains the children of the context node. The child axis is the default axis and it can be omitted. The descendant axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace nodes. //CCC/descendant::DDD Select elements DDD which have CCC among its ancestors /AAA Equivalent of /child::AAA
15
XPointer is intended to be the basis of fragment identifiers only for the text/xml and application/xml media types (they can point only to documents of these types). Pointing to fragments of remote documents is analogous to the use of anchors in HTML. Roughly: document#xpointer(…) xlink:type="simple"> xlink:href="mydocument.xml#xpointer(//AAA/BBB[1])">
16
If there are forbidden characters in your expression, you must deal with them somehow. When XPointer appears in an XML document, special characters must be escaped according to directions in XML. <linkxmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="test.xml#xpointer(//AAA position() < 2)"> Bzw. xlink:href="test.xml#xpointer(string-range('^(text in'))"> The characters < or & must be escaped using < and &. Any unbalanced parenthesis must be escaped using circumflex (^)
17
If your elements have an ID-type attribute, you can address them directly using the value of the ID-type attribute. (Don't forget: you must have an attribute defined as an ID type in your DTD!) Using ID-type attributes, you can easily include or jump to parts of documents. The example below selects node with id("b1"). xpointer(id("b1")) Bad book. Good book. Makes me sleep like a baby. All answers on only one page.
18
The specification defines one full form and one shorthand form (which is an abbreviation of the full one). Text in the first element BBB. Text in another element BBB. Text in more nested element. Text in more nested element. Text in more nested element. Again some text in some element. Short Form: /1/2/3 Full Form: xpointer(/*[1]/*[2]/*[3])
19
A location of type point is defined by a node, called the container node (node that contains the point), and a non-negative integer, called the index. (//AAA, //AAA/BBB are the container nodes, [1], [2] is used if more than one container node of the same name exists) xpointer(start-point(//AAA)) xpointer(start-point(range(//AAA/BBB[1]))) ▼ ▼ xpointer(end-point(range(//AAA/BBB[2]))) xpointer(start-point(range(//AAA/CCC)))
20
When the container node of a point is of a node type that cannot have child nodes (such as text nodes, comments, and processing instructions), then the index is an index into the characters of the string- value of the node; such a point is called a character- point. You can use this to write a link that behaves like a search function. It always jumps to the first appearance of a string, e.g. the word "another". xpointer(start-point(string-range(//*,'another', 2, 0))) Text in the first element BBB. Text in a▼nother element BBB. Text in more nested element. Again some text in some element.
21
The range function returns ranges covering the locations in the argument location-set. For each location x in the argument location-set, a range location representing the covering range of x is added to the result location set. xpointer(range(//AAA/BBB[2])) Text in another element BBB. The range-inside function returns ranges covering the contents of the locations in the argument location-set. xpointer(range-inside(//AAA/BBB[2])) Text in another element BBB.
22
For each location x in the argument location-set, end-point adds a location of type point to the result location-set. That point represents the end point of location x. xpointer(end-point(string-range(//AAA/BBB,'another'))) Text in the first element BBB. Text in another▼ element BBB. Text in more nested element. Again some text in some element.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.