xml language family detailed examples

27
XML Language Family Detailed Examples Most information contained in these slide comes from: h ttp://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 ([email protected]) This section contains examples on: XML Schema

Upload: zagiri

Post on 15-Jan-2016

19 views

Category:

Documents


0 download

DESCRIPTION

XML Language Family Detailed Examples. Most information contained in these slide comes from: h ttp://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 ([email protected]) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: XML Language Family Detailed Examples

XML Language FamilyDetailed Examples

• Most information contained in these slide comes from: h ttp://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 ([email protected])

• This section contains examples on:

XML Schema

Page 2: 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</myElement> correct<myElement type="integer">eT</myElement> 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 3: 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 4: 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 5: 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 6: 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 7: 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 8: 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 9: 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 10: 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 11: 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 12: 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 13: 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 14: 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 15: 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 16: 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 17: 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 18: 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 19: 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 20: 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="email" 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 21: 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 22: 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 23: 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 24: 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="./a"/>     <xsd:field xpath="@id"/> </xsd:key> <xsd:keyref name="myIdref" refer="myId">   <xsd:selector xpath="./b"/>     <xsd:field xpath="@ref"/> </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="ref" type="xsd:NCName" use="required"/>     </xsd:complexType>   </xsd:element>

Page 25: 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 26: 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 27: 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