1 / 99

XML Language Family Detailed Examples

XML Language Family Detailed Examples. Most information contained in these slide comes from: http://www.w3.org/ These slides are intended to be used as a tutorial on XML and related technologies Slide author: Jürgen Mangler etm@wkv.at This section contains examples on:

joel-hewitt
Download Presentation

XML Language Family Detailed Examples

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. XML Language FamilyDetailed Examples • Most information contained in these slide comes from: http://www.w3.org/ • These slides are intended to be used as a tutorial on XML and related technologies • Slide author:Jürgen Mangler etm@wkv.at • This section contains examples on: • XML, DTD (Document Type Definition) • XSchema • XPath, XPointer • XInclude, • XSLT • XLink

  2. The W3C is the "World Wide Web Consortium", a voluntary association of companies and non-profit organizations. Membership is very expensive but confers voting rights. The decisions of W3C are guided by the Advisory Committee, lead by Tim Berners-Lee. The XML recommendation was written by the W3C's XML Working Group (WC), which has since divided into a number of subgroups. • The stages in the life of a W3C Recommendation (REC) • Working Draft (maximum gap target: 3 months) • Last Call (public comment invited; W3C must respond) • Candidate Recommendation (design is stable; implementation feedback invited) • Proposed Recommendation (Advisory Committee review)

  3. An XML document is valid if it has an associated document type definition and if the document complies with the constraints expressed in it. The document type definition (DTD) must appear before the first element in the document. The name following the word DOCTYPE in the document type definition must match the name of the root element. tutorial.dtd: <!ELEMENT tutorial (#PCDATA)> tutorial.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE tutorial SYSTEM "tutorial.dtd"><tutorial>This is an XML document</tutorial>

  4. tutorial.dtd: <!ELEMENT XXX (AAA , BBB)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)> An element type has element content if elements of that type contain only child elements (no character data), optionally separated by white space. tutorial.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE XXX SYSTEM "tutorial.dtd"><XXX> <AAA>Start</AAA>   <BBB>End</BBB></XXX> tutorial.xml (with errors, BBB missing): <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE XXX SYSTEM "tutorial.dtd"><XXX> <AAA>Start</AAA></XXX>

  5. The root element XXX can contain zero or more elements AAA followed by precisely one element BBB. Element BBB must be always present.: If an element name in the DTD is followed by the star [*], this element can occur zero, once or several times tutorial.dtd: <!ELEMENT XXX (AAA* , BBB)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)> tutorial.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE XXX SYSTEM "tutorial.dtd"><XXX> <AAA>Start</AAA> <AAA>Again</AAA>   <BBB>End</BBB></XXX>

  6. tutorial.dtd: <!ELEMENT XXX (AAA+ , BBB)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)> If an element name in the DTD is followed by the plus [+], this element can occur once or several times. tutorial.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE XXX SYSTEM "tutorial.dtd"><XXX> <AAA>Start</AAA> <AAA>Again</AAA>   <BBB>End</BBB></XXX> tutorial.xml (with errors, AAA must occur at least once): <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE XXX SYSTEM "tutorial.dtd"><XXX>   <BBB>End</BBB></XXX>

  7. tutorial.dtd: <!ELEMENT XXX (AAA? , BBB)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)> If an element name in the DTD is followed by the question mark [?], this element can occur zero or one times. tutorial.xml: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE XXX SYSTEM "tutorial.dtd"><XXX>   <BBB>End</BBB></XXX> This example uses a combination of [ + * ?] <!ELEMENT XXX (AAA? , BBB+)><!ELEMENT AAA (CCC? , DDD*)><!ELEMENT BBB (CCC , DDD)><!ELEMENT CCC (#PCDATA)><!ELEMENT DDD (#PCDATA)> How could a valid document look like?

  8. The root element XXX must contain either one element AAA or one element BBB: With the character [ | ] you can select one from several elements. test.dtd: <!ELEMENT XXX (AAA | BBB)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)> test.xml: <!DOCTYPE XXX SYSTEM "test.dtd"><XXX>   <BBB>Valid</BBB></XXX> test.xml: <!DOCTYPE XXX SYSTEM "test.dtd"><XXX>   <AAA>Also Valid</AAA></XXX> Text can be interspersed with elements. <!ELEMENT XXX (AAA+ , BBB+)><!ELEMENT AAA (BBB | CCC )><!ELEMENT BBB (#PCDATA | CCC )*><!ELEMENT CCC (#PCDATA)>

  9. Attributes are used to associate name-value pairs with elements. Attribute specifications may appear only within start-tags and empty-element tags. The declaration starts with !ATTLIST followed by the name of the element (myElement) to which the attributes belong to, followed by the definition of the individual attributes (myAttributeA, myAttributeB). <!ELEMENT myElement (#PCDATA)><!ATTLIST myElement            myAttributeA  CDATA #REQUIRED           myAttributeB  CDATA #IMPLIED> <!DOCTYPE myElement SYSTEM "tutorial.dtd">       <myElement myAttributeA="#d1" myAttributeB="*~*">           Text       </myElement>

  10. An attribute of type CDATA may contain any arbitrary character data, given it conforms to well formedness constraints. Type NMTOKEN can contain only letters, digits and point [ . ] , hyphen [ - ], underline [ _ ] and colon [ : ] NMTOKENS can contain the same characters as NMTOKEN plus whitespaces. White space consists of one or more space characters, carriage returns, line feeds, or tabs. <!ELEMENT taskgroup (#PCDATA)><!ATTLIST  taskgroup  group  CDATA #IMPLIED purpose  NMTOKEN #REQUIRED names  NMTOKENS #REQUIRED> <!DOCTYPE persongroup SYSTEM "tutorial.dtd"><taskgroup group="#RT1" purpose="realisation::T1" names="Joe Max Eddie"/>

  11. The value of an attribute of type ID may contain only characters permitted for NMTOKEN and must start with a letter. No element type may have more than one ID attribute specified. The value of an ID attribute must be unique between all values of all ID attributes (in the document!). <!ELEMENT XXX (AAA+ , BBB+)>        <!ELEMENT AAA (#PCDATA)>        <!ELEMENT BBB (#PCDATA)>        <!ATTLIST AAA id ID #REQUIRED>         <!ATTLIST BBB               code ID #IMPLIED              list NMTOKEN #IMPLIED>       <XXX> <AAA id="a1"/>   <AAA id="a2"/>   <AAA id="a3"/>   <BBB code="QWQ-123-14-6" list="14:5"/></XXX>

  12. The value of an attribute of type IDREF has to match the value of some ID attribute in the document. The value of an IDREF attribute can contain several references to elements with ID attributes separated by whitespaces. <!ELEMENT XXX (AAA+ , CCC+)><!ELEMENT AAA (#PCDATA)><!ELEMENT CCC (#PCDATA)><!ATTLIST AAA mark ID #REQUIRED><!ATTLIST CCC ref IDREF #REQUIRED>        <XXX> <AAA mark="a1"/>   <AAA mark="a2"/>   <AAA mark="a3"/>   <CCC ref="a3" />   <CCC ref="a1 a2" /></XXX>

  13. <!ELEMENT XXX (AAA+, BBB+)>        <!ELEMENT AAA (#PCDATA)>        <!ELEMENT BBB (#PCDATA)>        <!ATTLIST AAA true ( yes | no ) #REQUIRED>        <!ATTLIST BBB month (1|2|3|4|5|6|7|8|9|10|11|12) #IMPLIED>        Permitted attribute values can be defined in the DTD If an attribute is implied, a default value can be provided in case the attribute isn't used. <!ELEMENT XXX (AAA+, BBB+)><!ELEMENT AAA (#PCDATA)><!ELEMENT BBB (#PCDATA)><!ATTLIST AAA true ( yes | no ) "yes"><!ATTLIST BBB month NMTOKEN "1"> #Required: You must set the attribute #Implied: You can set the attribute

  14. <!ELEMENT XXX (AAA+)><!ELEMENT AAA EMPTY><!ATTLIST AAA true ( yes | no ) "yes"> An element can be defined as EMPTY. In such a case it may contain attributes only but no text. <XXX> <AAA true="yes"/>   <AAA true="no"></AAA></XXX> <XXX>   <AAA true="yes"/>   <AAA true="no"></AAA>   <AAA>      </AAA> <AAA>Hello!</AAA></XXX> Are there errors in this example? Where are they?

  15. The purpose of XML Schema is to deploy a standard mechanism to describe and evaluate the datatype of the content of an element. XML examples: <myElement type="integer">12</AAA> correct<myElement type="integer">eT</AAA> also correct The XML Parser can not distiguish the content of an Element. This is where XML Schema comes in: <name xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> Jürgen Mangler </name>

  16. If we use the attribute "noNamespaceSchemaLocation", we tell the document that the schema belongs to an element from the null namespace. Valid document: <namexsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> Jürgen Mangler </name> correct_0.xsd: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="name" type="xsd:string"/> </xsd:schema>

  17. If we use the attribute "schemaLocation", we tell the document that the schema belongs to an element from some particular namespace. In the schema definition, you have to use the "targetNamespace" attribute, which defines the element's namespace. Valid document: <f:anElementxsi:schemaLocation="http://foo correct_0.xsd" xmlns:f="http://foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> This Element contains some cdata. </f:anElement> correct_0.xsd: <xsd:schema targetNamespace="http://foo" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="anElement" type="xsd:string"/> </xsd:schema>

  18. Valid document: <AAA> xxx yyy </AAA> If we want the root element to be named "AAA", from null namespace and containing text only. correct_0.xsd: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="AAA" type="xsd:string"/> </xsd:schema>

  19. If we want the root element to be named "AAA", from null namespace, containing text and an element "BBB", we will need to set the attribute "mixed" to "true" - to allow mixed content. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="AAA">     <xsd:complexType mixed="true">       <xsd:sequence minOccurs="1">         <xsd:element name="BBB" type="xsd:string"/>       </xsd:sequence>     </xsd:complexType>   </xsd:element> </xsd:schema> <AAA> xxx yyy   <BBB>ZZZ</BBB>aaa </AAA>

  20. We want the root element to be named "AAA", from null namespace, containing one "BBB" and one "CCC" element. Their order is not important. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="AAA">     <xsd:complexType mixed="false">       <xsd:all minOccurs="1" maxOccurs="1">         <xsd:element name="BBB" type="xsd:string"/>         <xsd:element name="CCC" type="xsd:string"/>       </xsd:all>     </xsd:complexType>   </xsd:element> </xsd:schema> <AAA>   <CCC/>   <BBB/> </AAA>

  21. We want the root element to be named "AAA", from null namespace, containing a mixture of any number (even zero), of "BBB" and "CCC" elements. We need to use the 'trick' below - we use a "sequence" element with "minOccurs" attribute set to 0 and "maxOccurs" set to "unbounded". The attribute "minOccurs" of the "element" elements has to be 0 too. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="AAA">     <xsd:complexType mixed="false">       <xsd:sequence minOccurs="0" maxOccurs="unbounded">         <xsd:element name="BBB" type="xsd:string" minOccurs="0"/>         <xsd:element name="CCC" type="xsd:string" minOccurs="0"/>       </xsd:sequence>     </xsd:complexType>   </xsd:element> </xsd:schema> Give a valid document!

  22. We want the root element to be named "AAA", from null namespace, containing a mixture of any number (even zero) of "BBB" and "CCC" elements. You need to use the trick below - use "sequence" element with "minOccurs" attribute set to 0 and "maxOccurs" set to "unbounded", and the attribute "minOccurs" of the "element" elements must be set to 0 too. <AAA>   <BBB>111</BBB>   <CCC>YYY</CCC>   <BBB>222</BBB>   <BBB>333</BBB>   <CCC>ZZZ</CCC> </AAA> A valid solution!

  23. We want the root element to be named "AAA", from null namespace, containing either "BBB" or "CCC" elements (but not both) - the "choice" element. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="AAA">     <xsd:complexType mixed="false">       <xsd:choice minOccurs="1" maxOccurs="1">         <xsd:element name="BBB" type="xsd:string"/>         <xsd:element name="CCC" type="xsd:string"/>       </xsd:choice>     </xsd:complexType>   </xsd:element> </xsd:schema> <AAA>   <CCC>aaa</CCC></AAA> Other valid solutions?

  24. In XML Schema, the datatype is referenced by the QName. The namespace must be mapped to the prefix. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="root" type="xsd:integer"/> </xsd:schema> <root> 25 </root>

  25. Restricting simpleType is relatively easy. Here we will require the value of the element "root" to be integer and less than 25. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="root">     <xsd:simpleType>       <xsd:restriction base="xsd:integer">         <xsd:maxExclusive value="25"/>       </xsd:restriction>     </xsd:simpleType>   </xsd:element> </xsd:schema> <root> 25 </root> Valid? Use <xsd:minInclusive value="0"/> to force element > 0. You can also combine min/max in <xsd:restriction>!

  26. If we want the element "root" to be either a string "N/A" or a string "#REF!", we will use <xsd:enumeration> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <xsd:element name="root">     <xsd:simpleType>       <xsd:restriction base="xsd:string">         <xsd:enumeration value="N/A"/>         <xsd:enumeration value="#REF!"/>       </xsd:restriction>     </xsd:simpleType>   </xsd:element> </xsd:schema> <root> N/A </root> Other solutions?

  27. If we want the element "root" to be either an integer or a string "N/A", we will make a union from an "integer" type and "string" type. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <xsd:element name="root">     <xsd:simpleType>       <xsd:union>         <xsd:simpleType>           <xsd:restriction base="xsd:integer"/>         </xsd:simpleType>         <xsd:simpleType>           <xsd:restriction base="xsd:string">             <xsd:enumeration value="N/A"/>           </xsd:restriction>         </xsd:simpleType>       </xsd:union>     </xsd:simpleType>   </xsd:element> </xsd:schema>

  28. Below we define a group of common attributes, which will be reused. The root element is named "root", it must contain the "aaa" element, and this element must have attributes "x" and "y".   <xsd:element name="root">     <xsd:complexType>       <xsd:sequence>         <xsd:element name="aaa" minOccurs="1" maxOccurs="1">           <xsd:complexType>             <xsd:attributeGroup ref="myAttrs"/>           </xsd:complexType>         </xsd:element>       </xsd:sequence>     </xsd:complexType>   </xsd:element> <xsd:attributeGroup name="myAttrs">     <xsd:attribute name="x" type="xsd:integer" use="required"/>     <xsd:attribute name="y" type="xsd:integer" use="required"/>   </xsd:attributeGroup> Give a valid document!

  29. Below we define a group of common attributes, which will be reused. The root element is named "root", it must contain the "aaa" and "bbb" elements, and these elements must have attributes "x" and "y". <root>   <aaa x="1" y="2"/> </root> Valid document from the previous Schema!

  30. We want the "root" element to have an attribute "xyz", which contains a list of three integers. We will define a general list (element "list") of integers and then restrict it (element "restriction") to have a length (element "length") of exactly three items. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="root">     <xsd:complexType>       <xsd:attribute name="xyz" use="required">         <xsd:simpleType>           <xsd:restriction base="myList">             <xsd:length value="3"/>           </xsd:restriction>         </xsd:simpleType>       </xsd:attribute>     </xsd:complexType>   </xsd:element> <xsd:simpleType name="myList">     <xsd:list itemType="xsd:integer"/>   </xsd:simpleType> </xsd:schema> Documents on next page …

  31. We want the "root" element to have an attribute "xyz", which contains a list of three integers. We will define a general list (element "list") of integers and then restrict it (element "restriction") to have a length (element "length") of exactly three items. Valid!<root xyz="0 0 1"/> Not valid! Why?<root xyz="0 0 1 1"/> Use the same method for lists in the content of elem.

  32. The element "A" has to contain a string which is exactly three characters long. We will define our custom type for the string named "myString" and will require the element "A" to be of that type. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >  <xsd:element name="A" type="myString"/>  <xsd:simpleType name="myString">     <xsd:restriction base="xsd:string">       <xsd:length value="3"/>     </xsd:restriction>   </xsd:simpleType> </xsd:schema> <buchstaben> abc </buchstaben>

  33. The element "A" must contain an email address. We will define our custom type, which will at least approximately check the validity of the address. We will use the "pattern" element, to restrict the string using regular expressions. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >   <xsd:element name="A" type="myString"/>   <xsd:simpleType name="myString">     <xsd:restriction base="xsd:string">       <xsd:pattern value="[^@]+@[^.]+\..+"/>     </xsd:restriction>   </xsd:simpleType> </xsd:schema> <email> kung@foo.org </email>

  34. Regular Expressions - the meaning of: [^@]+@[^.]+\..+ [abc] … Characters Class (character can be a, b or c) [^abc] … Negative Character Class (everything except a,b,c) * … Match 0 or more times + … Match 1 or more times ? … Match 1 or 0 times {n} … Match exactly n times {n,} … Match at least n times {n,m} … Match at least n but not more than m times . … match any character \w … Match a "word" character (alphanumeric plus "_") \W … Match a non-word character \d … Match a digit character \D … Match a non-digit character \. … Escape a character with a special Meaning (., +, *, ?, …) [^@]+ … match any character that is not a @ 1 or more times @ … match exactly a @ [^.]+ … match any character that is not a . 1 or more times \. … match exactly a . .+ … match any character 1 or more times

  35. One of the big problems of XML ist the type ID. An attribute of type ID must be unique for the whole file. XML Schema solves this problem: ID's can be vaild for a certain child axis only. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >  <xsd:element name="root" type="myList">     <xsd:unique name="myId">       <xsd:selector xpath="./a"/>       <xsd:field xpath="@id"/>     </xsd:unique>   </xsd:element>  <xsd:complexType name="myList">     <xsd:sequence minOccurs="1">       <xsd:element name="a" minOccurs="1" maxOccurs="unbounded">         <xsd:complexType>           <xsd:attribute name="id" type="xsd:NCName"/>         </xsd:complexType>       </xsd:element>     </xsd:sequence>   </xsd:complexType> </xsd:schema> Document on next page …

  36. One of the big problems of XML ist the type ID. An attribute of type ID must be unique for the whole file. XML Schema solves this problem: ID's can be vaild for a certain child axis only. <root>   <aid="x"/>   <aid="y"/>   <aid="z"/> </root>

  37. The "keyref" lets you specify, that an attribute/element refers to some node (which must be defined as "key" or "unique"). The "key" element requires the elements "a" under the "root" element to contain the existing and unique value of an "id" attribute. Replace <xsd:unique> with: <xsd:key name="myId">     <xsd:selector xpath="./AAA/a"/>     <xsd:field xpath="@id"/> </xsd:key> <xsd:keyref name="myIdref" refer="myId">   <xsd:selector xpath="./BBB/b"/>     <xsd:field xpath="@idref"/> </xsd:keyref> Add to <xsd:sequence> in myList:   <xsd:element name="b" minOccurs="0" maxOccurs="1">     <xsd:complexType>       <xsd:attribute name="idref" type="xsd:NCName" use="required"/>     </xsd:complexType>   </xsd:element> Document on next page …

  38. The "keyref" lets you specify, that an attribute/element refers to some node (which must be defined as "key" or "unique"). The "key" element requires the elements "a" under the "root" element to contain the existing and unique value of an "id" attribute. <root>   <a id="x"/>   <a id="y"/>   <b idref="x"/> </root>

  39. To define attributes AND childs for a certain element you have to use simpleContent. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"><xsd:element name="mixit">     <xsd:complexType>       <xsd:simpleContent>        <xsd:extension base="xsd:string">           <xsd:attribute name="times" type="xsd:string" use="required"/>         </xsd:extension>       </xsd:simpleContent>     </xsd:complexType>   </xsd:element> </xsd:schema> <mixit times="7" > shake </mixit>

  40. Wann nehm ich simple-, wann complexType • simpleType: • Wenn ich den Inhalt eines Elements als xsd:string, xsd:integer, xsd:double, ... definieren will • complexType • Wenn ich Attribute definieren will • Wenn ich andere Elemente als Inhalt definieren will (mit sequence, choice, all) • Wenn ich Attribute und Elemente mischen will (xsd:attribute unterhalt von sequence, choice, all • simpleContent innerhalb von complexType • Wenn ich den Inhalt eines Elements als Datentyp definieren und zusätzlich Attribute haben will

  41. 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. • 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.

  42. 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. • XPath models an XML document as a tree of nodes. There are different types of nodes, including element nodes, attribute nodes and text nodes.

  43. 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 Select the root element AAA       <AAA>           <BBB/>           <CCC/>           <BBB/>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC/>      </AAA> /AAA/CCC Select all elements CCC which are children of the root element AAA       <AAA>           <BBB/>           <CCC/>           <BBB/>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC/>      </AAA>

  44. If the path starts with // then all elements in the document, that fulfill the criteria following //, are selected. //BBB Select all elements BBB      <AAA>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC>                <DDD>                     <BBB/>                     <BBB/>                </DDD>           </CCC>      </AAA> //DDD/BBB Select all elements BBB which are children of DDD       <AAA>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC>                <DDD>                     <BBB/>                     <BBB/>                </DDD>           </CCC>      </AAA>

  45. The star * selects all elements located by the preceeding path /AAA/CCC/DDD/* Select all elements enclosed by elements /AAA/CCC/DDD       <AAA>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC>                <DDD>                     <BBB/>                     <BBB/>                </DDD>           </CCC>      </AAA> /*/*/*/BBB Select all elements BBB which have 3 ancestors      <AAA>            <CCC>                <DDD>                     <BBB/>                </DDD>           </CCC>           <CCC>                <DDD>                     <BBB/>                </DDD>           </CCC>      </AAA>

  46. 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. /AAA/BBB[1] Select the first BBB child of element AAA       <AAA>           <BBB/>           <BBB/>           <BBB/>           <BBB/>      </AAA> /AAA/BBB[last()] Select the last BBB child of element AAA       <AAA>           <BBB/>           <BBB/>           <BBB/>           <BBB/>      </AAA>

  47. //@id Select all attributes @id      <AAA>           <BBB id = "b1"/>           <BBB id = "b2"/>           <BBB name = "bbb"/>           <BBB/>      </AAA> //BBB[@id] Select BBB elements which have attribute id      <AAA>           <BBB id = "b1"/>           <BBB id = "b2"/>           <BBB name = "bbb"/>           <BBB/>      </AAA> Attributes are specified by @ prefix. //BBB[@*] Select BBB elements which have any attribute      <AAA>           <BBB id = "b1"/>           <BBB id = "b2"/>           <BBB name = "bbb"/>           <BBB/>      </AAA> //BBB[not(@*)] Select BBB elements without an attribute      <AAA>           <BBB id = "b1"/>           <BBB id = "b2"/>           <BBB name = "bbb"/>           <BBB/>      </AAA>

  48. 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. //BBB[@id='b1'] Select BBB elements which have attribute id with value b1       <AAA>           <BBB id = "b1"/>           <BBB name = " bbb "/>           <BBB name = "bbb"/>      </AAA> //BBB[normalize-space(@name)='bbb'] Select BBB elements which have an attribute name with value bbb, leading and trailing spaces are removed before comparison        <AAA>           <BBB id = "b1"/>           <BBB name = " bbb "/>           <BBB name = "bbb"/>      </AAA>

  49. //*[count(*)=3] Select elements which have 3 children    <AAA>           <CCC>                <BBB/>                <BBB/>                <BBB/>           </CCC>           <DDD>                <BBB/>           </DDD>           <EEE>                <CCC/>           </EEE>      </AAA> //*[count(BBB)=2] Select elements which have two children BBB      <AAA>           <CCC>                <BBB/>           </CCC>           <DDD>                <BBB/>                <BBB/>           </DDD>           <EEE>                <CCC/>                <DDD/>           </EEE>      </AAA> Function count() counts the number of selected elements

  50. Several paths can be combined with | separator ("|" stands for "or", like the logical or operator in C). AAA/EEE | //BBB Select all elements BBB and elements EEE which are children of root element AAA      <AAA>           <BBB/>           <CCC/>           <DDD>                <CCC/>           </DDD>           <EEE/>      </AAA> /AAA/EEE | //DDD/CCC | /AAA | //BBB Number of combinations is not restricted    <AAA>           <BBB/>           <CCC/>           <DDD>                <CCC/>           </DDD>           <EEE/>      </AAA>

More Related