99
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 [email protected] This section contains examples on: XML, DTD (Document Type Definition) – XSchema XPath, XPointer – XInclude, – XSLT – XLink

XML Language Family Detailed Examples

Embed Size (px)

DESCRIPTION

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 [email protected] This section contains examples on: - PowerPoint PPT Presentation

Citation preview

Page 1: XML Language Family Detailed Examples

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 [email protected]

• This section contains examples on:– XML, DTD (Document Type Definition) – XSchema– XPath, XPointer– XInclude, – XSLT– XLink

Page 2: XML Language Family Detailed Examples

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 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)

The XML recommendation was written by the W3C's XML Working Group (WC), which has since divided into a number of subgroups.

Page 3: XML Language Family Detailed Examples

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>

Page 4: XML Language Family Detailed Examples

An element type has element content if elements of that type contain only child elements (no character data), optionally separated by white space.

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>   <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>

Page 5: XML Language Family Detailed Examples

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>

The root element XXX can contain zero or more elements AAA followed by precisely one element BBB. Element BBB must be always present.:

Page 6: XML Language Family Detailed Examples

If an element name in the DTD is followed by the plus [+], this element can occur 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>

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>

Page 7: XML Language Family Detailed Examples

If an element name in the DTD is followed by the question mark [?], this element can occur zero or one 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>   <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?

Page 8: XML Language Family Detailed Examples

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>

The root element XXX must contain either one element AAA or one element BBB:

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)>

Page 9: XML Language Family Detailed Examples

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>

Page 10: XML Language Family Detailed Examples

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 #IMPLIEDpurpose  NMTOKEN #REQUIREDnames  NMTOKENS #REQUIRED>

<!DOCTYPE persongroup SYSTEM "tutorial.dtd"><taskgroup group="#RT1" purpose="realisation::T1" names="Joe Max Eddie"/>

Page 11: XML Language Family Detailed Examples

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>

Page 12: XML Language Family Detailed Examples

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>

Page 13: XML Language Family Detailed Examples

Permitted attribute values can be defined in the DTD

<!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>       

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

Page 14: XML Language Family Detailed Examples

An element can be defined as EMPTY. In such a case it may contain attributes only but no text.

<!ELEMENT XXX (AAA+)><!ELEMENT AAA EMPTY><!ATTLIST AAA true ( yes | no ) "yes">

<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?

Page 15: XML Language Family Detailed Examples

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>

Page 16: XML Language Family Detailed Examples

If we use the attribute "noNamespaceSchemaLocation", we tell the document that the schema belongs to an element from the null namespace.

Valid document:

<name xsi: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>

Page 17: XML Language Family Detailed Examples

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:anElement xsi: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>

Page 18: XML Language Family Detailed Examples

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>

Valid document:

<AAA> xxx yyy</AAA>

Page 19: XML Language Family Detailed Examples

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>

Page 20: XML Language Family Detailed Examples

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>

Page 21: XML Language Family Detailed Examples

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!

Page 22: XML Language Family Detailed Examples

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!

Page 23: XML Language Family Detailed Examples

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>

Other valid solutions?

<AAA>  <CCC>aaa</CCC></AAA>

Page 24: XML Language Family Detailed Examples

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>

Page 25: XML Language Family Detailed Examples

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>!

Page 26: XML Language Family Detailed Examples

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?

Page 27: XML Language Family Detailed Examples

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>

Page 28: XML Language Family Detailed Examples

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!

Page 29: XML Language Family Detailed Examples

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!

Page 30: XML Language Family Detailed Examples

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 …

Page 31: XML Language Family Detailed Examples

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.

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.

Page 32: XML Language Family Detailed Examples

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>

Page 33: XML Language Family Detailed Examples

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> [email protected] </email>

Page 34: XML Language Family Detailed Examples

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

Page 35: XML Language Family Detailed Examples

One of the big problems of XML ist the type ID. An attribute of type ID must be unique for the whole file.

<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>

XML Schema solves this problem: ID's can be vaild for a certain child axis only.

Document on next page …

Page 36: XML Language Family Detailed Examples

One of the big problems of XML ist the type ID. An attribute of type ID must be unique for the whole file.

<root>   <a id="x"/>   <a id="y"/>   <a id="z"/> </root>

XML Schema solves this problem: ID's can be vaild for a certain child axis only.

Page 37: XML Language Family Detailed Examples

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>

Document on next page …

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>

Page 38: XML Language Family Detailed Examples

<root>   <a id="x"/>   <a id="y"/>   <b idref="x"/> </root>

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.

Page 39: XML Language Family Detailed Examples

<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>

To define attributes AND childs for a certain element you have to use simpleContent.

<mixit times="7" > shake</mixit>

Page 40: XML Language Family Detailed Examples

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

Wann nehm ich simple-, wann complexType

Page 41: XML Language Family Detailed Examples

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.

Page 42: XML Language Family Detailed Examples

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

Page 43: XML Language Family Detailed Examples

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/CCCSelect all elements CCC which are children of the root element AAA 

     <AAA>           <BBB/>           <CCC/>           <BBB/>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC/>      </AAA>

/AAASelect the root element AAA 

     <AAA>           <BBB/>           <CCC/>           <BBB/>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC/>      </AAA>

Page 44: XML Language Family Detailed Examples

If the path starts with // then all elements in the document, that fulfill the criteria following //, are selected.

//DDD/BBBSelect all elements BBB which are children of DDD 

     <AAA>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC>                <DDD>                     <BBB/>                     <BBB/>                </DDD>           </CCC>      </AAA>

//BBBSelect all elements BBB 

     <AAA>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC>                <DDD>                     <BBB/>                     <BBB/>                </DDD>           </CCC>      </AAA>

Page 45: XML Language Family Detailed Examples

The star * selects all elements located by the preceeding path

/*/*/*/BBBSelect all elements BBB which have 3 ancestors

     <AAA>            <CCC>                <DDD>                     <BBB/>                </DDD>           </CCC>           <CCC>                <DDD>                     <BBB/>                </DDD>           </CCC>      </AAA>

/AAA/CCC/DDD/*Select all elements enclosed by elements /AAA/CCC/DDD 

     <AAA>           <BBB/>           <DDD>                <BBB/>           </DDD>           <CCC>                <DDD>                     <BBB/>                     <BBB/>                </DDD>           </CCC>      </AAA>

Page 46: XML Language Family Detailed Examples

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

     <AAA>           <BBB/>           <BBB/>           <BBB/>           <BBB/>      </AAA>

/AAA/BBB[1]Select the first BBB child of element AAA 

     <AAA>           <BBB/>           <BBB/>           <BBB/>           <BBB/>      </AAA>

Page 47: XML Language Family Detailed Examples

Attributes are specified by @ prefix. //BBB[@id]Select BBB elements which have attribute id

     <AAA>           <BBB id = "b1"/>           <BBB id = "b2"/>           <BBB name = "bbb"/>           <BBB/>      </AAA>

//@idSelect all attributes @id

     <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>

//BBB[@*]Select BBB elements which have any attribute

     <AAA>           <BBB id = "b1"/>           <BBB id = "b2"/>           <BBB name = "bbb"/>           <BBB/>      </AAA>

Page 48: XML Language Family Detailed Examples

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[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>

//BBB[@id='b1']Select BBB elements which have attribute id with value b1 

     <AAA>           <BBB id = "b1"/>           <BBB name = " bbb "/>           <BBB name = "bbb"/>      </AAA>

Page 49: XML Language Family Detailed Examples

Function count() counts the number of selected elements

//*[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>

Page 50: XML Language Family Detailed Examples

Several paths can be combined with | separator ("|" stands for "or", like the logical or operator in C).

/AAA/EEE | //DDD/CCC | /AAA | //BBBNumber of combinations is not restricted

   <AAA>           <BBB/>           <CCC/>           <DDD>                <CCC/>           </DDD>           <EEE/>      </AAA>

AAA/EEE | //BBBSelect all elements BBB and elements EEE which are children of root element AAA

     <AAA>           <BBB/>           <CCC/>           <DDD>                <CCC/>           </DDD>           <EEE/>      </AAA>

Page 51: XML Language Family Detailed Examples

Axes are a sophisticated concept in XML to find out which nodes relate to each other and how.

<parent> <preceding-sibling/> <preceding-sibling/> <node> <descendant/> <descendant/> </node> <following-sibling/> <following-sibling/><parent>

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

nodefollowing-sibling

preceding-sibling

descendant descendant

Page 52: XML Language Family Detailed Examples

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 one

• 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)

Page 53: XML Language Family Detailed Examples

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::DDDSelect elements DDD which have CCC among its ancestors

     <CCC>           <DDD>                <EEE>                     </DDD>                </EEE>           </DDD>      </CCC>

/AAAEquivalent of /child::AAA

     <AAA>           <BBB/>           <CCC/>      </AAA>

Page 54: XML Language Family Detailed Examples

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(…)

<link xmlns:xlink="http://www.w3.org/2000/xlink">    xlink:type="simple"> xlink:href="mydocument.xml#xpointer(//AAA/BBB[1])"></link>

Page 55: XML Language Family Detailed Examples

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.

<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"

xlink:href="test.xml#xpointer(//AAA position() &lt; 2)">Bzw.

xlink:href="test.xml#xpointer(string-range('^(text in'))"></link>

• The characters < or & must be escaped using &lt; and &amp;.

• Any unbalanced parenthesis must be escaped using circumflex (^)

Page 56: XML Language Family Detailed Examples

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"))

<AAA>   <BBB myid="b1" bbb="111">Text in the first element BBB.</BBB>   <BBB myid="b2" bbb="222"> Text in another element BBB. <DDD ddd="999">Text in more nested element.</DDD>   </BBB>   <CCC ccc="123" xxx="321">Again some text in some element.</CCC></AAA>

Page 57: XML Language Family Detailed Examples

The specification defines one full form and one shorthand form (which is an abbreviation of the full one).

<AAA>   <BBB myid="b1" bbb="111">Text in the first element BBB.</BBB>   <BBB myid="b2" bbb="222"> Text in another element BBB.       <DDD ddd="999">Text in more nested element.</DDD>      <DDD ddd="888">Text in more nested element.</DDD>      <DDD ddd="777">Text in more nested element.</DDD>   </BBB>   <CCC ccc="123" xxx="321">Again some text in some element.</CCC> </AAA>

• Short Form: /1/2/3• Full Form: xpointer(/*[1]/*[2]/*[3])

Page 58: XML Language Family Detailed Examples

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])))

<AAA>▼   <BBB bbb="111"></BBB>   <BBB bbb="222"> <DDD ddd="999"></DDD>   </BBB>   <CCC ccc="123" xxx="321"/> </AAA>

<AAA>   <BBB bbb="111"></BBB>   <BBB bbb="222"> <DDD ddd="999"></DDD>   </BBB>▼   <CCC ccc="123" xxx="321"/> </AAA>

xpointer(end-point(range(//AAA/BBB[2]))) xpointer(start-point(range(//AAA/CCC)))

Page 59: XML Language Family Detailed Examples

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)))

<AAA>   <BBB bbb="111">Text in the first element BBB.</BBB>   <BBB bbb="222"> Text in a▼nother element BBB. <DDD ddd="999">Text in more nested element.</DDD>   </BBB>   <CCC ccc="123" xxx="321">Again some text in some element.</CCC></AAA>

Page 60: XML Language Family Detailed Examples

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]))

<AAA>   <BBB bbb="111"/> <BBB bbb="222">   Text in another element BBB.   </BBB>    <CCC ccc="123" xxx="321"/></AAA>

The range-inside function returns ranges covering the contents of the locations in the argument location-set.

xpointer(range-inside(//AAA/BBB[2]))

<AAA>   <BBB bbb="111"/> <BBB bbb="222">   Text in another element BBB.   </BBB>    <CCC ccc="123" xxx="321"/></AAA>

Page 61: XML Language Family Detailed Examples

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')))

 <AAA>   <BBB bbb="111">Text in the first element BBB.</BBB>   <BBB bbb="222"> Text in another▼ element BBB. <DDD ddd="999">Text in more nested element.</DDD>   </BBB>   <CCC ccc="123" xxx="321">Again some text in some element.</CCC> </AAA>

Page 62: XML Language Family Detailed Examples

XInclude solves the problem of including external documents / parts of external documents into a XML-Document.

It works quite like an #include in c/c++ does, only that you can include documents trough http locators, and include parts of documents (trough xpointer syntax).

In other words, this technology provides a way to split your work to logical pieces, and tie the end result back together (we are using a book example, with tied together chapters, in our slides)

Page 63: XML Language Family Detailed Examples

XInclude references external documents to be included with include elements in the http://www.w3.org/2001/XInclude namespace. The prefix xi is customary though not required. Each xi:include element has an href attribute that contains a URL pointing to the file to include.

<?xml version="1.0"?><book xmlns:xi="http://www.w3.org/2001/XInclude"> <title>The Wit and Wisdom of George W. Bush</title> <xi:include href="malapropisms.xml"/> <xi:include href="mispronunciations.xml"/> <xi:include href="madeupwords.xml"/></book>

To resolve XIncludes, a document must be passed through an XInclude processor that replaces the xi:include elements with the documents they point to.

Page 64: XML Language Family Detailed Examples

XInclude processing is recursive. That is, an included document can itself include another document. For example, a book might be divided into front matter, back matter, and several parts:

<?xml version="1.0"?><book xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="frontmatter.xml"/> <xi:include href="part1.xml"/> <xi:include href="part2.xml"/></book>

Each part might be further divided into a part intro and several chapters:

<?xml version="1.0"?><book xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include href="ch01.xml"/> <xi:include href="ch02.xml"/></book>

Circular inclusion (Document A includes Document B which includes, directly or indirectly, Document A) is forbidden.

Page 65: XML Language Family Detailed Examples

Technical articles like this one often need to include example code: programs, XML and HTML documents, e-mail messages, and so on. Within these examples characters like < and & should be treated as raw text rather than parsed as markup.

To include a document as plain text, you have to add a parse="text" attribute to the xi:include element.

For example, this fragment loads the source code for the Java program SpellChecker.java from the examples directory into a code element:

<code> <xi:include parse="text" href="examples/SpellChecker.java" /></code>

Page 66: XML Language Family Detailed Examples

For many reasons, documents included from remote servers may be temporarily unavailable.The default action for an XInclude processor in such a case is simply to give up and report a fatal error. However, the xi:include element may contain an xi:fallback element which contains alternate content to be used if the requested resource cannot be found. <xi:include href="http://www.whitehouse.gov/malapropisms.xml"> <xi:fallback> <para>We're making the right decisions to bring the solution to an end.</para> </xi:fallback></xi:include>

The xi:fallback element can even include another xi:include element.

Page 67: XML Language Family Detailed Examples

To resolve XIncludes, a document must be passed through an XInclude processor that replaces the xi:include elements with the documents they point to.

To include parts of other documents you can useThe xpointer scheme (see xpointer part).

Software Support:• Libxml, <http://xmlsoft.org/> includes fairly complete support for XInclude.• The 4Suite XML library for Python <http://4suite.org/> has an option to resolve

XIncludes when parsing.• GNU JAXP <http://www.gnu.org/software/classpathx/jaxp/> includes a SAX

filter that resolves XIncludes, provided no XPointers are used.

Page 68: XML Language Family Detailed Examples

With XSL you can freely modify the content and/or layout any source text. You can apply different Stylesheets to the same source to get different results.

<source> <title>XSL</title> <author>John Smith</author> </source>

<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="/">     <h1><xsl:value-of select="//title"/></h1>       <h2><xsl:value-of select="//author"/></h2> </xsl:template></xsl:stylesheet>

Output:<h1>John Smith</h1><h2>XSL</h2>

How can you produce the following output?<h2>John Smith</h2><h1>XSL</h1>

Page 69: XML Language Family Detailed Examples

Every XSL stylesheet must start with an xsl:stylesheet element. The attribute version='1.0' specifies version of XSL(T) specification. This example shows the simplest possible stylesheet. As it does not contain any information, default processing is used.

<source> <em>Hello, world</em></source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'></ xsl:stylesheet>

Output:Hello, world

Page 70: XML Language Family Detailed Examples

An XSL processor parses an XML source and tries to find a matching template rule. If it does, instructions inside the matching template are evaluated.

<source> <bold>Hello, world.</bold> <red>I am </red> <italic>fine.</italic> </source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="bold"> <p><b><xsl:value-of select="."/></b></p> </xsl:template> <xsl:template match="red"> <p style="color:red"><xsl:value-of select="."/></p> </xsl:template> <xsl:template match="italic"> <p><i><xsl:value-of select="."/></i></p> </xsl:template></xsl:stylesheet>

Output<p><b>Hello, world.</b></p><p style="color:red">I am </p><p><i>fine.</i></p>

Page 71: XML Language Family Detailed Examples

Contents of the original elements can be recovered from the original sources in two basic ways.

Stylesheet 1 uses xsl:value-of construct. In this case the contents of the element is used without any further processing.

The instruction xsl:apply-templates in Stylesheet 2 is different. The parser further processes selected elements, for which a template is defined.

<source> <employee>     <firstName>Joe</firstName>       <surname>Smith</surname> </employee> </source>

For the next page we use this source

Page 72: XML Language Family Detailed Examples

Stylesheet 1:<xsl:stylesheet version='1.0‚ xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="employee">       <b><xsl:value-of select="."/></b> </xsl:template> <xsl:template match="surname">       <i><xsl:value-of select="."/></i> </xsl:template></xsl:stylesheet>

Stylesheet 2:<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="employee">       <b><xsl:apply-templates select="firstName"/></b>       <b><xsl:apply-templates select="surname"/></b> </xsl:template> <xsl:template match="surname">     <i><xsl:value-of select="."/></i> </xsl:template></xsl:stylesheet>

<b>Joe</b><b><i>Smith</i></b>

<b>JoeSmith</b>

Which Stylesheet produces which Output?

Page 73: XML Language Family Detailed Examples

<source> <AAA id="a1" pos="start">       <BBB id="b1"/>       <BBB id="b2"/> </AAA> <AAA id="a2">       <BBB id="b3"/>       <BBB id="b4"/>       <CCC id="c1">          <DDD id="d1"/>       </CCC> </AAA> </source>

Parts of an XML document to which a template should be applied are determined by location paths. The required syntax is specified in the XPath specification. Simple cases looks very similar to filesystem addressing.

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="/source/AAA/CCC/DDD">     <p style="color:red">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </p> </xsl:template></xsl:stylesheet>

Output<p style="color:red">DDD id=d1</p>

Page 74: XML Language Family Detailed Examples

Processing always starts with the template match="/". This matches the root node (the node whose only child element is the document element, in our case "source"). Many stylesheets do not contain this element explicitly.

When this template is not explicitly given, the implicit template is used (it contains as the sole instruction).

<xsl:template match="*|/"><xsl:apply-templates/>

</xsl:template>

This instruction means: process all children of the current node, including text nodes.

Page 75: XML Language Family Detailed Examples

<source> <AAA id="a1" pos="start">       <BBB id="b1"/>       <BBB id="b2"/> </AAA> <AAA id="a2">       <BBB id="b3"/>       <BBB id="b4"/>       <CCC id="c1">          <DDD id="d1"/>       </CCC> </AAA> </source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="AAA">     <div style="color:purple">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> </xsl:template> <xsl:template match="BBB">       <div style="color:blue">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> </xsl:template> </xsl:stylesheet>

Output<div style="color:purple">AAA id=a1</div> <div style="color:purple">AAA id=a2</div>

Parses only first level under <source>

Page 76: XML Language Family Detailed Examples

<source> <AAA id="a1" pos="start">       <BBB id="b1"/>       <BBB id="b2"/> </AAA> <AAA id="a2">       <BBB id="b3"/>       <BBB id="b4"/>       <CCC id="c1">          <DDD id="d1"/>       </CCC> </AAA> </source>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:template match="AAA">     <div style="color:purple">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> <xsl:apply-templates/> </xsl:template> <xsl:template match="BBB">       <div style="color:blue">          <xsl:value-of select="name()"/>          <xsl:text> id=</xsl:text>          <xsl:value-of select="@id"/>       </div> </xsl:template> </xsl:stylesheet>

Output<div style="color:purple">AAA id=a1</div> <div style="color:blue">BBB id=b1</div> <div style="color:blue">BBB id=b2</div> <div style="color:purple">AAA id=a2</div> <div style="color:blue">BBB id=b3</div> <div style="color:blue">BBB id=b4</div>

Recurses into sublevels of <AAA>

Page 77: XML Language Family Detailed Examples

A template can match individual paths being separated with "|" ( Stylesheet 1) from a selection of location paths. Wildcard "*" selects all possibilities. Compare Stylesheet 1 with Stylesheet 2.

Stylesheet 1:<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="firstName|surname"> <div>       <xsl:text>[template: </xsl:text>            <xsl:value-of select="name()"/>            <xsl:text> outputs </xsl:text>            <xsl:apply-templates/>            <xsl:text> ]</xsl:text>      </div> </xsl:template></xsl:stylesheet>

<source> <employee>       <firstName>Joe</firstName>       <surname>Smith</surname> </employee> </source>

Output:<div>[template: firstName outputs Joe ]</div><div>[template: surname outputs Smith ]</div>

Page 78: XML Language Family Detailed Examples

A template can match individual paths being separated with "|" ( Stylesheet 1) from a selection of location paths. Wildcard "*" selects all possibilities. Compare Stylesheet 1 with Stylesheet 2.Stylesheet 2:<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="*"> <div>       <xsl:text>[template: </xsl:text>            <xsl:value-of select="name()"/>            <xsl:text> outputs </xsl:text>            <xsl:apply-templates/>            <xsl:text> ]</xsl:text>      </div> </xsl:template></xsl:stylesheet>

<source> <employee>       <firstName>Joe</firstName>       <surname>Smith</surname> </employee> </source>

Output:<div>[template: source outputs <div>[template: employee outputs <div>[template: firstName outputs Joe ]</div>     <div>[template: surname outputs Smith ]</div>]</div>]</div>

Page 79: XML Language Family Detailed Examples

With modes an element can be processed multiple times, each time producing a different result.

Stylesheet 2:<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <xsl:apply-templates select="//CCC" mode="red"/>      <xsl:apply-templates select="//CCC"/> </xsl:template><xsl:template match="CCC" mode="red">      <div style="color:red">           <xsl:value-of select="name()"/>      </div> </xsl:template><xsl:template match="CCC">      <div style="color:purple">           <xsl:value-of select="name()"/>      </div> </xsl:template></xsl:stylesheet>

<source><AAA id="a2">      <CCC id="c1">           <CCC id="c2"/>

     </CCC>      <BBB id="b5">           <CCC id="c3"/>

     </BBB> </AAA></source>

Output:<div style="color:red">CCC</div><div style="color:red">CCC</div><div style="color:red">CCC</div><div style="color:purple">CCC</div><div style="color:purple">CCC</div><div style="color:purple">CCC</div>

Page 80: XML Language Family Detailed Examples

Axes play a very important role in XSLT – e.g. child axis, for-each. Stylesheet 2:

<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <table border="1" cellpadding="6">           <xsl:for-each select="/source/*">                <xsl:call-template name="print"/>           </xsl:for-each>      </table> </xsl:template><xsl:template name="print">      <tr>           <td> <xsl:value-of select="./@id"/>                 <xsl:text>: </xsl:text>

<xsl:for-each select="child::*">                      <xsl:value-of select="./@id"/>                      <xsl:text> </xsl:text>                </xsl:for-each>           </td>      </tr> </xsl:template> </xsl:stylesheet>

Document:<source><AAA id="a2">      <BBB id="b4"/>      <CCC id="c1"/>

</AAA></source>

Output:<table border="1" cellpadding="6"> <tr>      <td>a2: b4 c1</td> </tr></table>

Page 81: XML Language Family Detailed Examples

xsl:element generates elements in time of processing. In this example it transforms the sizes to formating tags. <xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <xsl:for-each select="//text">           <xsl:element name="{@size}">               <xsl:value-of select="."/>          </xsl:element>      </xsl:for-each> </xsl:template></xsl:stylesheet>

<source> <text size="H1">Header1</text> <text size="H3">Header3</text> <text size="b">Bold text</text> <text size="sub">Subscript</text> <text size="sup">Superscript</text> </source>

Output:<H1>Header1</H1><H3>Header3</H3><b>Bold text</b><sub>Subscript</sub><sup>Superscript</sup>

Page 82: XML Language Family Detailed Examples

xsl:if instruction enables conditional processing. A typical case of xsl:for-each usage is to add a text between individual entries. Very often you do not want to add text after the last element:

<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="source">      <xsl:for-each select="entry">           <xsl:value-of select="@name"/>           <xsl:if test="not (position()=last())">                <xsl:text>, </xsl:text>           </xsl:if>      </xsl:for-each> </xsl:template> </xsl:stylesheet>

<source> <entry name="A"/>    <entry name="B"/>    <entry name="C"/> <entry name="D"/></source>

Output:A, B, C, D

Page 83: XML Language Family Detailed Examples

Numbering of individual chapter elements depends on the position of the chapter element. Each level of chapters is numbered independently. Setting the attribute level to multiple enables natural numbering.

<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match="/">      <TABLE BORDER="1">           <xsl:for-each select="//chapter">                <TR>                    <TD>                          <xsl:number level="multiple"/>                          <xsl:text> - </xsl:text> <xsl:value-of select="./text()"/>                     </TD>               </TR>           </xsl:for-each>      </TABLE> </xsl:template> </xsl:stylesheet>

<source><chapter>First Chap.</chapter> <chapter>Sec. Chap.      <chapter>Sub 1</chapter>      <chapter>Sub 2</chapter> </chapter> </source>

What could be the possible output?Continued on next page.

Page 84: XML Language Family Detailed Examples

Numbering of individual chapter elements depends on the position of the chapter element. Each level of chapters is numbered independently. Setting the attribute level to multiple enables natural numbering.

Output:<TABLE BORDER="1">  <TR>     <TD>1 - First Chap.</TD>  </TR>  <TR>     <TD>2 - Sec. Chap.</TD>  </TR>  <TR>     <TD>2.1 - Sub 1</TD>  </TR>  <TR>     <TD>2.2 - Sub 2</TD>  </TR>  </TABLE>

Page 85: XML Language Family Detailed Examples

You can set variables in a Stylesheet and use them later in the processing. The following example demonstrates a way of setting xsl:variable.<xsl:stylesheet version='1.0'   xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name="totalChapters">      <xsl:value-of select="count(//chapter)"/> </xsl:variable> <xsl:template match="/">      <TABLE>           <xsl:for-each select="//chapter">                <TR><TD>                    <xsl:value-of select="."/>                    <xsl:value-of select="position()"/>                    <xsl:text>/</xsl:text>                    <xsl:value-of select="$totalChapters"/>                </TD></TR>           </xsl:for-each>      </TABLE> </xsl:template> </xsl:stylesheet>

<source> <chapter>Chapter</chapter> <chapter>Chapter</chapter> <chapter>Chapter</chapter> <chapter>Chapter</chapter> </source>

What could be the possible output?Continued on next page.

Page 86: XML Language Family Detailed Examples

You can set variables in a Stylesheet an use them later in the processing. The following example demonstrates a way of setting xsl:variable.

Output:<TABLE>  <TR>     <TD>Chapter 1/4</TD>  </TR>  <TR>     <TD>Chapter 2/4</TD>  </TR>  <TR>     <TD>Chapter 3/4</TD>  </TR>  <TR>     <TD>Chapter 4/4</TD>  </TR></TABLE>

Page 87: XML Language Family Detailed Examples

There currently exist 2 sorts of XLinks

Note: Currently only few Browsers (Amaya, Mozilla [recommend]) have support for XLinks.

• Simple XLinks• Extended XLinks

Extended XLinks are a method to describe the dependency of resources in general. Therefore an extended link is a link that associates an arbitrary number of resources. The participating resources may be any combination of remote and local ones.

Simple XLinks are intended specifically to replace normal HTML <a> tags.

Page 88: XML Language Family Detailed Examples

The use of XLink elements and attributes requires the declaration of the XLink namespace.

<?xml version="1.0"?>  <AAA xmlns:xlink="http://www.w3.org/1999/xlink">   <BBB xlink:type="extended"> Any content here   </BBB></AAA>

For the namespace declarations reserved attributes starting with xmlns are used.

The namespace declaration given for the current element is also valid for all elements occuring inside the current one (for all children and descendants).

Page 89: XML Language Family Detailed Examples

It is possible to specify default attribute values in the DTD. Attributes then do not have to appear physically on element start-tags.

<!ELEMENT AAA:logo (#PCDATA)>

<!ATTLIST AAA:logo xmlns:xlink CDATA #FIXED "http://www.w3.org/1999/xlink" xlink:type (simple) #FIXED "simple"xlink:href CDATA #FIXED "sample.gif" xlink:show (embed) #FIXED "embed" xlink:actuate (onLoad) #FIXED "onLoad" >

<AAA:logo xmlns:AAA = "http://www.sample.org"> A logo</AAA:logo> Simple XLinks

Simple XLinks are intended to be a more flexible way of creating links in HTML documents, and are scheduled to replace the <a></a> tags in normal HTML.

Page 90: XML Language Family Detailed Examples

If the attribute "show" is set to "new", a new window is opened for displaying the link. When combined with attribute actuate equal to onLoad, the window is opened immediately. Because the element logo is empty, there are no visible links in the page.

<AAA:logo xmlns:AAA = "http://www.zvon.org" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple"

xlink:href="sample.gif"xlink:show="new"xlink:actuate="onLoad">

</AAA:logo>

Simple XLinks

Page 91: XML Language Family Detailed Examples

• show = newactuate = onRequest A new window is opened for displaying the link, the window is not opened automatically.

• show = replaceactuate = onRequestThe link is opened in the current window, the window is not opened automatically.

• show = embedactuate = onLoadThe link replaces the element link, replacement is immediate (similarly to the well-known element <html:img src=... >)

• show = replaceactuate = onLoadThe link replaces the current document, replacement is immediate.

Simple XLinks

Page 92: XML Language Family Detailed Examples

Extended XLinks don't serve only the purpose of mere document linking, but also provide a formalized way to describe, what the link is pointing to.In the case of the later used "Greek myth" example you will realize that somone can't only link to a page about Heracles, but also the link contains more generalized information about Greek heroes at all.

So every Extended XLink is not only a pointer to content, but also a pointer to information what the link is all about.

In the Greek myth example, every link holds also information about relatives, context, stories … of a referred person.

Extended XLinks

Page 93: XML Language Family Detailed Examples

An element of type "locator" indicates a remote resource:

<AAA xmlns:xlink="http://www.w3.org/1999/xlink"xlink:type="extended">

<BBB xlink:type="locator"      xlink:href="http://www.sample.org/xxl/XSLT/Output/index.html">   </BBB></AAA>

Extended XLinks

• Elements of type "locator" must have the attribute "href" and its value must be supplied.

• Elements of type "locator" must have the extended-type element as a parent, otherwise they have no specified meaning in XLink.

Page 94: XML Language Family Detailed Examples

An element of type "resource" indicates a local resource.

<AAA xmlns:xlink="http://www.w3.org/1999/xlink"xlink:type="resource">

Anything here. <BBB> Any content.   </BBB></AAA>

Extended XLinks

• Resource-type element may have any content. This content has no Xlink-specified relationship to the link.

• It is possible for resource-type element to have no content.

Page 95: XML Language Family Detailed Examples

The extended-, locator- and arc-type elements may have the title attribute. But they may also have a series of one or more title-type elements.

<AAA xmlns:xlink="http://www.w3.org/1999/xlink"xlink:type="locator"xlink:title="Greek Heros">

   <AAA_title      xlink:type="title"      xml:lang="en">     Heracles fight against the giants   </AAA_title>   <AAA_title      xlink:type="title"      xml:lang="de">     Heracles Kampf gegen die Riesen </AAA_title></AAA>

Extended XLinks

This example shows how to distinguish between users speaking different languages.

Page 96: XML Language Family Detailed Examples

The attribute role describes the meaning of the resource.

The value of the role attribute must be a URI reference. It identifies some resource that describes the intended property.

<hero xmlns:xlink="http://www.w3.org/1999/xlink"xlink:type="locator"xlink:href="http://www.greece.antique/database/heracles.xml"xlink:role="http://www.greece.antique/hero"xlink:title="Heracles">

</hero> <god xmlns:xlink="http://www.w3.org/1999/xlink"

xlink:type="locator"xlink:href="http://www.greece.antique/database/zeus.xml"xlink:role="http://www.greece.antique/god"xlink:title="Zeus">

</god> Extended XLinks

Page 97: XML Language Family Detailed Examples

The attribute label provides "marks", to which the attributes from and to refer.

The value of the role attribute must be a URI reference. It identifies some resource that describes the intended property.

<woman xmlns:xlink="http://www.w3.org/1999/xlink"xlink:type="locator"xlink:href="http://www.greece.antique/database/alcmene.xml"xlink:role="http://www.greece.antique/woman"xlink:label="alcmene"xlink:title="Alcmene">

</woman>

Extended XLinks

Page 98: XML Language Family Detailed Examples

An element of type "arc" indicates rules for traversing among resources participating in extended-type link.

<mythology xmlns:xlink="http://www.w3.org/1999/xlink">   <god xlink:type="locator"

xlink:href="http://www.greece.antique/database/zeus.xml"xlink:role="http://www.greece.antique/god"xlink:title="Zeus"xlink:label="zeus">

   </god>   <hero xlink:type="locator"

xlink:href="http://www.greece.antique/database/heracles.xml"xlink:role="http://www.greece.antique/hero"xlink:title="Heracles"xlink:label="heracles">

   </hero>   <relation xlink:type="arc"

xlink:from="zeus"xlink:to="heracles">

   </relation></mythology>

Extended XLinks

Page 99: XML Language Family Detailed Examples

The attribute arcrole describes the meaning of the arc-type element.

<relation      xmlns:xlink="http://www.w3.org/1999/xlink"xlink:type="arc"xlink:from="zeus"xlink:to="heracles"xlink:arcrole="http://www.greece.antique/relations/father">

</relation>

Extended XLinks

The attributes from and to refer to "marks", which are set by attribute label.