Difference between revisions of "XML Schema"
(→Valid xml) |
(→Hello World) |
||
Line 48: | Line 48: | ||
<title>atitle</title> | <title>atitle</title> | ||
</head> | </head> | ||
+ | </source> | ||
+ | |||
+ | ==Enumeration== | ||
+ | ===schema=== | ||
+ | <source lang="xml"> | ||
+ | <?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> | ||
+ | </source> | ||
+ | ===valid xml=== | ||
+ | <source lang="xml"> | ||
+ | <monatsname>Jan</monatsname> | ||
</source> | </source> | ||
Revision as of 17:46, 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>
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
}