Difference between revisions of "XML Schema"

From no name for this wiki
Jump to: navigation, search
(Resourcen)
(Resourcen)
Line 97: Line 97:
  
 
== Resourcen ==
 
== Resourcen ==
[http://de.wikipedia.org/wiki/XML_Schema wikipedia]
+
* [http://de.wikipedia.org/wiki/XML_Schema wikipedia]

Revision as of 17:47, 14 September 2009

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>

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
}

Resourcen