Subject:flexible XSD for extra tags in xml? Author:Mark Lane Date:04 Oct 2005 08:44 AM
Right now my XSD looks like this :(message is defined in xsd too)
<xs:element name="Publish">
<xs:complexType>
<xs:sequence>
<xs:element ref="Message" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
My XML message that I validate against the XSD looks like this :
<?xml version="1.0" encoding="utf-8" ?>
<RtiMsg>
<Header>
<Dest>16</Dest>
<Source></Source>
<UserId>RAMORTON</UserId>
<Version>1.0</Version>
<MsgId>13</MsgId>
<MsgType>Publish</MsgType>
</Header>
<Publish>
<Message>send to only 13</Message>
</Publish>
</RtiMsg>
What I want is to be able to have xml tags created underneath the <Message> tag at runtime. With no limitation of what the new tags are named or how many tags are contained in the Publish Messagetype.
So that the xml message will still be validated by the xsd
<?xml version="1.0" encoding="utf-8" ?>
<RtiMsg>
<Header>
<Dest>16</Dest>
<Source></Source>
<UserId>RAMORTON</UserId>
<Version>1.0</Version>
<MsgId>13</MsgId>
<MsgType>Publish</MsgType>
</Header>
<Publish>
<Message>send to only 13</Message>
<newtag>not explicitly named in the xsd</newtag>
<anytagIwant>tag name will only be known at runtime</anytagIwant>
<asmanytags>as I want</asmanytags>
</Publish>
</RtiMsg>
The key is to use
<xsd:any processContents="skip" maxOccurs="unbounded" minOccurs="0"/>
In your case processContents can be "skip" or "lax". First instructs
XML processor not to validate extra elements at all, second instructs XML processor to obtain schema for the element namespace, but if no schema available XML processor will not generate an error.