XSD Basic Help Needed -- Namespaces in Schemas?
I've got something like the following XML file.
<credits>
<property name="tag">
<item>v0003</item>
</property>
<property
name="tag">
<item>mhma03h</item>
</property>
</credits>
First requirement is that this XML cannot change no matter what. Please don't suggest doing that below.
I need to write a schema that validates this. And Java code that does the validation.
I am completely stuck and I have no idea really what is going on.
What's a schema like this look like? I've gotten one but it's so bad I'm not going to bother posting. :P I don't want to have to append a namespace to the XML elements. They're set in stone.^_^
How do I just make it so all of these elements are just "found" by the parser? Can I just tell it ignore all this namespace nonsense. With this application of validating against a schema, namespace conflicts are simply impossible.
I've tried putting
<xsd:schema xmlns:xsd="http://w开发者_运维问答ww.w3.org/2001/XMLSchema" xmlns="books" elementFormDefault="unqualified">
for my namespace info and
UPDATE: I've updated what I'm doing to reflect the answers given so far! :)
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:element ref="item"/>
</xs:sequence>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="mhma03h"/>
<xs:enumeration value="v0003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="credits">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML v0003
Code to load and validate yes. Before you ask, the files are able to be loaded. I've checked like 20 times. :P
SAXParserFactory factory = SAXParserFactory.newInstance();
SchemaFactory schemaFactory =
SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
factory.setSchema(schemaFactory.newSchema(
new Source[] {new StreamSource("small.xsd")}));
javax.xml.parsers.SAXParser parser = factory.newSAXParser();
org.xml.sax.XMLReader reader = parser.getXMLReader();
reader.setFeature("http://xml.org/sax/features/validation", true);
reader.setFeature("http://apache.org/xml/features/validation/schema", true);
reader.parse(new InputSource("small.xml"));
Here is a schema that corresponds to the XML file you posted:
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2011 sp1 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="property">
<xs:complexType>
<xs:sequence>
<xs:element ref="item"/>
</xs:sequence>
<xs:attribute name="name" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="mhma03h"/>
<xs:enumeration value="v0003"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="credits">
<xs:complexType>
<xs:sequence>
<xs:element ref="property" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This may not correctly capture the requirements for each element and attribute (check that the min/max occurs, required/optional proprerties, etc... are set correctly) but it should get you started on working with an XML schema that will validate correctly. The schema does not define a target namespace so you won't have to worry about modifying the existing XML to add in namespace prefixes to your existing elements.
You need to find a way to tell the XML processor to use your namespace as the default when it is processing a document without an explicit namespace. Most processors have a way to do this, IIRC there is a method named setNoNamespaceSchema
or something similar. You would write an XML schema with a namespace and tell the processor (validator, whatever) to use your namespace for documents that do not have an explicit namespace.
精彩评论