XML Schema

From no name for this wiki
Jump to: navigation, search

XML Schema Samples

Tool

Hier ein kleines Tool um Schemas zu testen: start


Hello World

Schema

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="html">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="head"/>
        <xsd:element name="body" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="head">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="title" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Valid xml

Beispiel1:

<html>
<head>
<title>atitle</title>
</head>
<body>mymody</body>
</html>

Beispiel 2:

<head>
<title>atitle</title>
</head>

Enumeration

schema

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
  <xsd:element name="monatsname">
   <xsd:simpleType>
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="Jan"/>
      <xsd:enumeration value="Feb"/>   
    </xsd:restriction>
   </xsd:simpleType>
  </xsd:element>
 
</xsd:schema>

valid xml

<monatsname>Jan</monatsname>


Complex type

schema

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
<xsd:element name="employee" type="personinfo"/>
 
<xsd:complexType name="personinfo">
   <xsd:sequence>
    <xsd:element name="firstname" type="xsd:string"/>
    <xsd:element name="lastname" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
 
</xsd:schema>

valid xml

<employee>
  <firstname>Claude</firstname>
  <lastname>Glauser</lastname>
</employee>


group und all

schema

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
<xs:group name="custGroup">
  <xs:all>
    <xs:element name="cust" type="xs:string"/>
    <xs:element name="op" type="xs:string"/>
  </xs:all>
</xs:group>
 
<xs:element name="order" type="ordertype"/>
 
<xs:complexType name="ordertype">
  <xs:group ref="custGroup"/>
  <xs:attribute name="status" type="xs:string"/>
</xs:complexType>
 
</xs:schema>

valid xml

  <order>
    <cust/>
    <op/>
  </order>

Extending a tpye

schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
  <xs:element name="address" type="UKAddressType"/>
 
  <xs:complexType name="AddressType">
    <xs:sequence>
        <xs:element name="Line1" type="xs:string"/> 
        <xs:element name="Line2" type="xs:string"/>
    </xs:sequence> 
 </xs:complexType> 
 
 <xs:complexType name="UKAddressType"> 
    <xs:complexContent> 
        <xs:extension base="AddressType"> 
            <xs:sequence> 
                <xs:element name="County" type="xs:string"/> 
                <xs:element name="Postcode" type="xs:string"/> 
            </xs:sequence> 
        </xs:extension> 
    </xs:complexContent> 
 </xs:complexType> 
</xs:schema>

valid xml

Valides XML:

<address>
  <Line1/>
  <Line2/>
  <County/>
  <Postcode/>
</address>

Die Elemente von UKAddressType kommen am Schluss. Folgendes XML wäre ungültig:

<address>
  <County/>
  <Postcode/>
  <Line1/>
  <Line2/>
</address>

Attributes

Attribute können nur zu complex types hinzugefügt werden.

schema

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="html">
    <xsd:complexType>
      <xsd:attribute name="lang" type="xsd:string"/>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

valid xml

<html lang="English"/>


Key und Keyref

Es ist möglich, Primärschlüssel und Fremdschlüssel zu definieren.

schema

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
 
<xsd:complexType name="pc-Typ">
  <xsd:sequence>
    <xsd:element name="prozessor"  type="xsd:string"/>  
  </xsd:sequence>
  <xsd:attribute name="id" type="xsd:integer"/>
</xsd:complexType>
 
 
<xsd:element name="pc-list">
 
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="computer" type="pc-Typ" maxOccurs="unbounded"/>
 
      <xsd:element name="alterrechner" minOccurs="0"  maxOccurs="unbounded">
        <xsd:complexType>
          <xsd:attribute name="id" type="xsd:integer"/>	
        </xsd:complexType>
 
     </xsd:element>
 
    </xsd:sequence>
  </xsd:complexType>
 
  <xsd:key name="idKey">
    <xsd:selector xpath="computer"/>
    <xsd:field xpath="@id"/>
  </xsd:key>
 
  <xsd:keyref name="idFremdKey" refer="idKey">
    <xsd:selector xpath="alterrechner"/>
    <xsd:field xpath="@id"/>
  </xsd:keyref>
 
</xsd:element>
 
</xsd:schema>

valid xml

Folgendes ist ein valides XML:

<pc-list>
 
 <computer id="88">
  <prozessor>Interl 486</prozessor>
 </computer>
 
 <alterrechner id="88"/>
 
</pc-list>

Folgendes XML ist ungültig, da es computer mit id 688 nicht gibt:

<pc-list>
 
 <computer id="88">
  <prozessor>Interl 486</prozessor>
 </computer>
 
 <alterrechner id="688"/>
 
</pc-list>

Namespaces

form unqualified

schema

<xsd:schema targetNamespace="http://www.example.com"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="whole">
       <xsd:complexType>
           <xsd:sequence>
               <xsd:element name="part" type="xsd:string" form="unqualified"/>
           </xsd:sequence>
       </xsd:complexType>
   </xsd:element>
</xsd:schema>

valid xml

<myns:whole xmlns:myns="http://www.example.com">
  <part/>
</myns:whole>


list

schema

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
  <xsd:element name="html">
    <xsd:complexType>
      <xsd:sequence>    
         <xsd:element name="myname" type="foo"/>	
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
 
  <xsd:complexType name="foo">
     <xsd:attribute name="items">
         <xsd:simpleType>
           <xsd:list itemType="xsd:int"/>
         </xsd:simpleType>
    </xsd:attribute>
  </xsd:complexType>
 
</xsd:schema>

valid xml

<html>
  <myname items="1 2 888"/>
</html>

Union

schema

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://mynamespace"
        xmlns:myns="http://mynamespace">
 
<element name="whole">
<complexType>
 <sequence>
  <element name="part" form="qualified" maxOccurs="unbounded">
   <simpleType>
     <union memberTypes="myns:myInt myns:myString"/>
   </simpleType>
  </element>
 </sequence>
</complexType>
</element>
 
<simpleType name="myInt">
<restriction base="int">
 <enumeration value="1"/>
 <enumeration value="2"/>
</restriction>
</simpleType>
 
<simpleType name="myString">
<restriction base="string">
 <enumeration value="ab"/>
 <enumeration value="bc"/>
</restriction>
</simpleType>    
 
</schema>

valid xml

<whole xmlns="http://mynamespace">
 <part>1</part>
 <part>ab</part>
 <part>bc</part>
</whole>

NIL

schema

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://mynamespace"
        xmlns:myns="http://mynamespace">
 
<element name="whole">
<complexType>
 <sequence>
  <element name="part" form="qualified" maxOccurs="unbounded" nillable="true">
   <simpleType>
     <restriction base="int">
       <enumeration value="1"/>
       <enumeration value="2"/>
     </restriction>   
   </simpleType>
  </element>
 </sequence>
</complexType>
</element>
 
</schema>

valid xml

<whole xmlns="http://mynamespace" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <part xsi:nil="true"/>
</whole>

Java Code zum validieren eines Schemas

@Override 
protected Object doInBackground() {
 
  try {
    this.message = null;
    java.io.StringReader schemaReader = new java.io.StringReader(this.schematext);
    java.io.StringReader xmlReader = new java.io.StringReader(this.xmltext);
    javax.xml.transform.stream.StreamSource schemaSource = new javax.xml.transform.stream.StreamSource(schemaReader);
    javax.xml.transform.stream.StreamSource xmlSource = new javax.xml.transform.stream.StreamSource(xmlReader);
    javax.xml.validation.SchemaFactory
       factory = javax.xml.validation.SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    javax.xml.validation.Schema schema = factory.newSchema(schemaSource);
    javax.xml.validation.Validator validator = schema.newValidator();
    validator.validate(xmlSource); //Throws an exception on invalid xml.
  }
  catch(Exception e){
    this.message = e.getMessage();
  }            
  return null;  // return your result
}

XML Datentypen

  • string
  • boolean
  • float
  • double
  • decimal
  • dateTime
  • duration
  • hexBinary
  • base64Binary
  • anyURI
  • ID
  • IDREF
  • ENTITY
  • NOTATION
  • normalizedString: xs:normalizedString does not allow carriage return (#xD), line feed (#xA) nor tab (#x9) characters as a part of values.
  • token
  • language
  • IDREFS
  • NMTOKEN
  • NMTOKENS
  • Name
  • QName
  • NCName: NCName = non-colonized name. Is a name that is not qualified with a namespace-related prefix. Eg: <Order/>
  • integer
  • nonPositiveInteger
  • negativeInteger
  • byte
  • int
  • long
  • short
  • unsignedByte
  • unsignedInt
  • unsignedLong
  • date
  • time
  • gYearMonth
  • gYear
  • gMonthDay
  • gDay
  • gMonth

Resourcen