I have a scenario where I need a xsd which is used for validating few bunch of xml's which have few things in common. For example,
xml1:
<myXml1>
<myTag>
<someTag>..</someTag>
<requiredTag>..</requiredTag>
<someotherTag>..</someotherTag>
</myTag>
<myXml1>
xml2:
<myXml1>
<myTag>
<requiredTag>..</requiredTag>
<otherTag>..</otherTag>
</myTag>
<myXml1>
I am in need of a common xsd so as to validate both the xmls's which have "requiredTag" in common and read its value.
I tried using <xs:any> but it is supported only in xs:sequence and not in xs:all. My requiredTag can be anywhere under myTag.
I would like to use this xsd to create classes using jaxb for reading the value.
Any help is appreciated.
Thanks in advance. :)
If you can validate with XSD 1.1, you can use:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:complexType name="myTagType">
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
<xs:element name="requiredTag" type="xs:string"/>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>
</xs:complexType>
...
</xs:schema>
Unfortunately this fails in XSD 1.0 because it violates the Unique Particle Atribution constraint.
One workaround consists in using different namespaces for the any elements and requiredTag. Declare a schema with a namespace for the required tag:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
xmlns="required-tag-ns"
targetNamespace="required-tag-ns">
<xs:element name="requiredTag" type="xs:string"/>
</xs:schema>
Now import that into your schema (you will need a prefix for one of them):
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="any-tag-ns"
xmlns="any-tag-ns"
xmlns:r="required-tag-ns">
<xs:import namespace="required-tag-ns" schemaLocation="required.xsd"/>
<xs:complexType name="myTagType">
<xs:sequence>
<xs:any namespace="any-tag-ns" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
<xs:element ref="r:requiredTag"/> <!-- now it works in 1.0 because the ambiguity was removed -->
<xs:any namespace="any-tag-ns" minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
</xs:sequence>
</xs:complexType>
...
</xs:schema>
Either the XSD 1.1 schema, or the XSD 1.0 schema above will validate these cases:
<myTag>
<requiredTag>..</requiredTag>
</myTag>
<myTag>
<someTag>..</someTag>
<requiredTag>..</requiredTag>
</myTag>
<myTag>
<requiredTag>..</requiredTag>
<someotherTag>..</someotherTag>
</myTag>
<myTag>
<someTag>..</someTag>
<someotherTag>..</someotherTag>
<someotherTag>..</someotherTag>
<requiredTag>..</requiredTag>
<someotherTag>..</someotherTag>
</myTag>
<myTag>
<someTag>..</someTag>
<requiredTag>..</requiredTag>
<someTag>..</someTag>
<someTag>..</someTag>
<someotherTag>..</someotherTag>
</myTag>
But they will not validate these cases:
<myTag> <!-- missing requiredTag -->
<someTag>..</someTag>
<someotherTag>..</someotherTag>
</myTag>
<myTag> <!-- too many requiredTags -->
<requiredTag>..</requiredTag>
<requiredTag>..</requiredTag>
<someotherTag>..</someotherTag>
</myTag>
In the XSD 1.1 version you won't need the namespaces. In the XSD 1.0 solution your instance will have to qualify the requiredTag with its namespace:
<myXml xmlns="any-tag-ns"
xmlns:r="required-tag-ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="any-tag-ns your-schema-file.xsd">
<myTag>
<someTag>..</someTag>
<someotherTag>..</someotherTag>
<someotherTag>..</someotherTag>
<r:requiredTag>..</r:requiredTag>
<someotherTag>..</someotherTag>
</myTag>
...
</myXml>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With