By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

XML schema validation question restricting"attributes" count

P: n/a
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>
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Victor wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
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
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:40********@olaf.komtel.net...


Victor wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/


Jul 20 '05 #3

P: n/a
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
"John Smith" <Jo**********@hotmail.com> wrote in message
news:10****************@iris.uk.clara.net...
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
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:40********@olaf.komtel.net...


Victor wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/


Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.