Reputation: 305
I have an XML that looks like this:
<Artifacts count="2">
<Artifact>
...
</Artifact>
<Artifact>
...
</Artifact>
</Artifacts>
I am looking for a way to enforce that the number of Artifact
elements contained in Artifact
s shall be equal to the value of the "count" attribute by using an XSD schema.
Even though I found possible ways to achieve this by using the XSD 1.1 specification, I wonder if it is at all possible without it, i.e based on the XSD 1.0 specification.
Edit: I will try to provide a little more context to the question in order to be more precise.
The XML file will be provided as input to a C++ application. The problem is that the development environment enforces the usage of the Xerces v. 2.8.0 library for parsing. To my understanding this version does not support the XSD 1.1 standard.
Of course, I can add extra code in order to check for the correct number of occurrences of Artifact
elements after the XSD validation. I was hoping that there would be a way to avoid the extra code segment and completely validate the input file based on the XSD alone.
Upvotes: 4
Views: 612
Reputation: 22617
The correct term for the kind of constraint you'd like to model is assertion. No, assertions are not possible in XML Schema 1.0. The only validation languages that support assertions are XML Schema 1.1 (xs:assert
) and Schematron (sch:assert
and sch:report
).
Other than that, you could write XPath expressions or XSLT stylesheets that test for the correct number of Artifact
elements:
/Artifacts/@count = count(/Artifacts/Artifact)
The XML file will be provided as input to a C++ application. The problem is that the development environment enforces the usage of the Xerces v. 2.8.0 library for parsing. To my understanding this version does not support the XSD 1.1 standard.
My guess is that Xerces 2.8.0 (an outdated version that is no longer supported) does not conform to XSD 1.1, but the easiest way to find out is to simply test it. Include any xs:assert
in a schema and see what happens.
Of course, I can add extra code in order to check for the correct number of occurrences of "Artifact" elements after the XSD validation. I was hoping that there would be a way to avoid the extra code segment and completely validate the input file based on the XSD alone.
No, with only XSD 1.0 you cannot avoid those extra lines of code.
EDIT What @kjhughes wrote in a comment should actually be part of the answer:
This is correct, and I would also challenge OP's design that requires an attribute that merely mirrors the number of child attributes.
From the point of view of XML design, if this attribute does nothing else than state how many Artifact
elements there are, why did you include it in the first place? You should always be reluctant to store redundant and recoverable information. For instance, there is no need to store the position of child elements like so:
<root>
<child n="1"/>
<child n="2"/>
</root>
And your count
attribute does something very similar.
Upvotes: 2
Reputation: 2340
I'll assume that count value is dynamic based on actual number of Artifact elements. One way (since I don't know what's your context) could be to modify the XSD with minOccurs and maxOccurs according with this value and then validate against the new schema...
<xsd:complexType name="Artifacts">
<xsd:sequence>
<xsd:element name="Artifact" minOccurs="COUNT_VALUE" maxOccurs="COUNT_VALUE" />
</xsd:sequence>
</xsd:complexType>
Upvotes: 1