Validating only part of XML document using schema?
Is it possible to write a schema allowing mixing validated XML with just well-formed XML? Case in point is a data transfer application where requests consist of a bunch of meta-data (which we'd like to validate) and record-specific information (which is validated separately from the XML in the business logic, because the XML requests are just one of several ways through which requests can come in).
Example:
<request>
<campaign>CAMP00001</campaign>
<username>user_bob</username>
<token>one-time-token-goes-here</token>
... more meta-data ...
<records>
<record id="90209">
<name>John Doe</name>
<address>Park Lane 191</address>
<postal>99999</postal>
</record>
<record id="90210">
<name>Jane Doe</name>
<address>Park Lane 192</address>
<postal>88888</postal>
</record>
</records>
</request>
Currently I'm validating by manually checking for the presence and validity of the content of the campaign, username and token fields. If possible, I'd like to do t开发者_开发技巧his using an XML schema so that it becomes easier to communicate to developers what meta-data they need to supply and in which formats - for campaigns, this is impossible as each campaign has its own set of fields and rules that are applied to those. Can such a thing be done?
(one solution I thought of is filling record with a set of <data>
elements instead of named elements, and then have a very loose definition of <data>
in the schema.. but that'd require changing the specs around)
You can use the Any type in a schema.
For Example
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="first" type="xs:string"/>
<xs:element name="second" type="xs:string"/>
<xs:any minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
This requires each foo element to have a first and second node and then allows any other elements as well. For example:
<foo>
<first>Hello</first>
<second>World</second>
<other>can be anything</other>
<nodes/>
</foo>
You could put the validated and unvalidated stuff in different namespaces and provide a pretty wide-open schema for the unvalidated content.
精彩评论