Difference between revisions of "XML Schema"
(→Resourcen) |
(→Complex type) |
||
Line 96: | Line 96: | ||
<lastname>Glauser</lastname> | <lastname>Glauser</lastname> | ||
</employee> | </employee> | ||
+ | </source> | ||
+ | |||
+ | ==Attributes== | ||
+ | Attribute können nur zu complex types hinzugefügt werden. | ||
+ | ===schema=== | ||
+ | <source lang="xml"> | ||
+ | <?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> | ||
+ | </source> | ||
+ | ===valid xml=== | ||
+ | <source lang="xml"> | ||
+ | <html lang="English"/> | ||
</source> | </source> | ||
Revision as of 21:28, 14 September 2009
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>
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
}