HighTechTalks DotNet Forums  

Can a schema be written to check for this?

Dotnet XML microsoft.public.dotnet.xml


Discuss Can a schema be written to check for this? in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Mr Flibble
 
Posts: n/a

Default Can a schema be written to check for this? - 06-15-2006 , 07:51 AM







Here is some highly imaginative XML I've constructed to highlight what
it is I'm trying to achieve. (Seems easier with an example).

<foobars>
<foo>
<bar x="dog" colour="brown"/>
<bar x="cat" colour="grey"/>
<bar x="fish" colour="gold"/>
</foo>
<foo>
<bar x="dog" colour="black"/>
<bar x="cat" colour="white"/>
<bar x="fish" colour="blue"/>
</foo>
</foobars>

I want to ENFORCE that every <foo> contains three <bar> children and
that "dog", "cat", and "fish" appear each once. It's easy to to say
that <bar> must occur max=3 and min=3 but I cannot specify that "dog",
"cat", and "fish" appear since I cannot define <bar> more than once in
my schema. I get some "duplicate declaration within current scope" error.

Now if I re-formed my XML document thusly:

<foobars>
<foo>
<bar>
<dog>brown</dog>
<cat>grey</cat>
<fish>gold</fish>
</bar>
</foo>

<foo>
<bar>
<dog>brown</dog>
<cat>grey</cat>
<fish>gold</fish>
</bar>
</foo>

</foobars>


It's possible to do because there is only one <bar> and <dog>, <cat>,
and <fish> all have different names and I can check that each one occurs
once and only once.

The thing is if I change my XML I have to change my app, so at the
moment my preferred solution is to find a way of writing a schema that
works for example #1.

Can it be done?

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Can a schema be written to check for this? - 06-15-2006 , 08:19 AM








Mr Flibble wrote:

Quote:
Here is some highly imaginative XML I've constructed to highlight what
it is I'm trying to achieve. (Seems easier with an example).

foobars
<foo
<bar x="dog" colour="brown"/
<bar x="cat" colour="grey"/
<bar x="fish" colour="gold"/
</foo
<foo
<bar x="dog" colour="black"/
<bar x="cat" colour="white"/
<bar x="fish" colour="blue"/
</foo
/foobars

I want to ENFORCE that every <foo> contains three <bar> children and
that "dog", "cat", and "fish" appear each once.
XSD does not allow you to put such constraints on attribute values. As
you have recognized yourself the proper way is to make dog, cat, fish
element names of elements your document contains.
If you want to put a constraint on attribute values then you might want
to try Schematron, it is a schema language that allows you to specify
assertions with the help of XPath. Schematron is however not supported
directly by any Microsoft XML parser. With the help of an XSLT processor
Schematron schemas can be checked.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Stan Kitsis [MSFT]
 
Posts: n/a

Default Re: Can a schema be written to check for this? - 06-16-2006 , 01:34 PM



Yes, you can do this using uniqueness constraints. The following snippet
does what you need. You need to make sure your schema declares a target
namespace (I assumed its prefix is "tns").

<xs:simpleType name="animalType">
<xs:restriction base="xs:string">
<xs:enumeration value="dog"/>
<xs:enumeration value="cat"/>
<xs:enumeration value="fish"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="barType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="x" type="animalType"/>
<xs:attribute name="colour" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>

<xs:complexType name="fooType">
<xs:sequence minOccurs="3" maxOccurs="3">
<xs:element name="bar" type="barType"/>
</xs:sequence>
</xs:complexType>

<xs:element name="foobars">
<xs:complexType>
<xs:sequence>
<xs:element name="foo" type="fooType" maxOccurs="unbounded">
<xs:unique name="uniqueAnimal">
<xs:selector xpath="./tns:bar"/>
<xs:field xpath="@x"/>
</xs:unique>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


"Mr Flibble" <mr.flibble (AT) flibbyly (DOT) wobbly.n.et> wrote

Quote:
Here is some highly imaginative XML I've constructed to highlight what
it is I'm trying to achieve. (Seems easier with an example).

foobars
foo
bar x="dog" colour="brown"/
bar x="cat" colour="grey"/
bar x="fish" colour="gold"/
/foo
foo
bar x="dog" colour="black"/
bar x="cat" colour="white"/
bar x="fish" colour="blue"/
/foo
/foobars

I want to ENFORCE that every <foo> contains three <bar> children and
that "dog", "cat", and "fish" appear each once. It's easy to to say
that <bar> must occur max=3 and min=3 but I cannot specify that "dog",
"cat", and "fish" appear since I cannot define <bar> more than once in
my schema. I get some "duplicate declaration within current scope" error.

Now if I re-formed my XML document thusly:

foobars
foo
bar
dog>brown</dog
cat>grey</cat
fish>gold</fish
/bar
/foo

foo
bar
dog>brown</dog
cat>grey</cat
fish>gold</fish
/bar
/foo

/foobars


It's possible to do because there is only one <bar> and <dog>, <cat>,
and <fish> all have different names and I can check that each one occurs
once and only once.

The thing is if I change my XML I have to change my app, so at the
moment my preferred solution is to find a way of writing a schema that
works for example #1.

Can it be done?



Reply With Quote
  #4  
Old   
Mr Flibble
 
Posts: n/a

Default Re: Can a schema be written to check for this? - 06-19-2006 , 11:25 AM



* Stan Kitsis [MSFT] wrote:
Quote:
Yes, you can do this using uniqueness constraints.
Thank you , that worked a TREAT!


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.