XML schema validation question restricting"attributes" count

V

Victor

Hi, I have some sample XML and an XSD below I have written.

The XSD almost does what I want. What I need is some way of enforcing
that AT LEAST TWO of the attributes "TestAttribute" are "X".

Can this be done and if so how?
Thank you
Victor



<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="TestXML">
<xs:complexType>
<xs:sequence minOccurs="2" maxOccurs="4">
<xs:element name="TestElement">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="TestAttribute" type="xs:string"
use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


XML that is valid, 2 TestAttribute="X" (2 satisfies "AT LEAST TWO")
===================================================================

<?xml version="1.0" encoding="UTF-8"?>
<TestXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\test.xsd">
<TestElement TestAttribute="3">Mark</TestElement>
<TestElement TestAttribute="5">Peter</TestElement>
<TestElement TestAttribute="X">John</TestElement>
<TestElement TestAttribute="X">Luke</TestElement>
</TestXML>


XML that is valid, 3 TestAttribute="X" (3 satisfies "AT LEAST TWO")
===================================================================

<?xml version="1.0" encoding="UTF-8"?>
<TestXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\test.xsd">
<TestElement TestAttribute="3">Mark</TestElement>
<TestElement TestAttribute="X">Peter</TestElement>
<TestElement TestAttribute="X">John</TestElement>
<TestElement TestAttribute="X">Luke</TestElement>
</TestXML>


XML that does not conform to the modified schema above
1 TestAttribute="X" (1 DOES NOT satisfy "AT LEAST TWO")
===================================================================

<?xml version="1.0" encoding="UTF-8"?>
<TestXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\test.xsd">
<TestElement TestAttribute="3">Mark</TestElement>
<TestElement TestAttribute="5">Peter</TestElement>
<TestElement TestAttribute="7">John</TestElement>
<TestElement TestAttribute="X">Luke</TestElement>
</TestXML>
 
M

Martin Honnen

Victor said:
The XSD almost does what I want. What I need is some way of enforcing
that AT LEAST TWO of the attributes "TestAttribute" are "X".

Can this be done and if so how?

The W3C schema language allows uniqueness and key constraints but none
of those can in my view express the constraint of at least two
attributes having a certain value.
 
J

John Smith

I see. I couldn't not find anything on the W3 website either.

I can redesign my application to use an XML schema that dictated ONE AND
ONLY ONE of the "TestElement" had an attribute that was "X" (ie if there
existed more than one "TestElement" in "TestXML" with an attribute "X" then
the XML was invalid, if there existed no "TestElement" in "TestXML" with an
attribute "X" then the XML it would be invalid).

Can this done?


By the way, thank you very much for putting me on the right track the other
day.
Victor
 
J

John Smith

I'm thinking along the lines of

<xs:field xpath="count(//.@TestAttribute="X")=1/>

but I just can't get it to work properly yet.

Thanks
Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top