Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML schema validation question restricting"attributes" count

Reply
Thread Tools

XML schema validation question restricting"attributes" count

 
 
Victor
Guest
Posts: n/a
 
      02-12-2004
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 xmlnss="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 xmlnssi="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 xmlnssi="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 xmlnssi="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>
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-12-2004


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/

 
Reply With Quote
 
 
 
 
John Smith
Guest
Posts: n/a
 
      02-12-2004
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" <> wrote in message
news:402b616b$...
>
>
> 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/
>




 
Reply With Quote
 
John Smith
Guest
Posts: n/a
 
      02-12-2004
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" <> wrote in message
news:...
> 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" <> wrote in message
> news:402b616b$...
> >
> >
> > 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/
> >

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to xml schema - does the dtd/schema validation happens always ? pramodr XML 3 04-05-2009 12:10 PM
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
Validation with XSD using XML::LibXML::Schema, and XML::Validator::Schema huntingseasonson@gmail.com Perl Misc 5 11-29-2006 12:37 PM
Problem with schema-validation and property "http://apache.org/xml/properties/schema/external-schemaLocation" Markus Java 1 11-23-2005 02:41 PM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57