XML Schema
XML Schema Samples
Contents
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>
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
<address>
<Line1/>
<Line2/>
<County/>
<Postcode/>
</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"/>
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
}