Suppose I have a XML schema and want to support some extensions at several nodes. An extension should be valid XML within these nodes.
I know this might be implemented with the <any> element in you schema. However In my XML that uses my schema I want this extension to only use nodes from an other XSD. So specifying the Schema of the extension at runtime and then being able to validate this extension against the extension schema.
The following example uses a static extension schema:
<xs:element name="notes" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:any namespace="http://www.w3.org/1999/xhtml"
minOccurs="0" maxOccurs="unbounded"
processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Now I want to specify this schema in my XML, for example (I'am a newbie), like this:
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLoc开发者_JAVA百科ation="myschema.xsd">
<bar>
<extension>
<html namespace="http://www.w3.org/1999/xhtml">
<body>Hello, World!</body>
</html>
</extension>
</bar>
</foo>
What is the best approach for this? Ideally I want to have a list in my XML of the schema's I use in the extension nodes of my XML.
Thanks!
== Edited, more detailed explanation: ==
I want to support user defined XML data within specific nodes. I don't know the schema of these extension during the writing of my "master" schema.
I have specified the following fragment in my XSD:
<xs:element name="extension">
<xs:complexType>
<xs:sequence>
<xs:any namespace="##other" processContents="strict"/>
</xs:sequence>
</xs:complexType>
</xs:element>
And want to use the following XML:
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd" >
<bar>
<extension>
<html xmlns="http://www.w3.org/1999/xhtml">
<body2>Hello, world!</body2>
</html>
</extension>
</bar>
</foo>
Now I do want an parser error because <body2 is not an valid XHTML element. However the XMLSpy parser is already complaining about the <html> element that is not valid.
Use "strict" processing to mandate that the contents of the any element is validated against its schema, i.e.
<xs:element name="notes" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:any namespace="http://www.w3.org/1999/xhtml"
minOccurs="0" maxOccurs="unbounded"
processContents="strict"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Validation of your document should then fail, since the processor is likely unable to obtain a schema for XHTML. You would need to specify this in the instance document also, like so:
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test.xsd">
<extension>
<html xmlns="http://www.w3.org/1999/xhtml"
xsi:schemaLocation="http://www.w3.org/1999/xhtml
http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd">
<body2>Hello, world!</body2>
</html>
</extension>
</foo>
Now, many XSD validator will still choke on this, since they don't consider the schemaLocation attribute on the html element (although they ought to, per spec). Moving the schemaLocation attribute into the root element might (or might not) improve things: if the validator complains about the html element, it's broken.
精彩评论