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).
<bar x="dog" colour="brown"/>
<bar x="cat" colour="grey"/>
<bar x="fish" colour="gold"/>
<bar x="dog" colour="black"/>
<bar x="cat" colour="white"/>
<bar x="fish" colour="blue"/>
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:
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?
Re: Can a schema be written to check for this? - 06-15-2006 , 08:19 AM
Mr Flibble wrote:
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
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:attribute name="x" type="animalType"/>
<xs:attribute name="colour" type="xs:string"/>
<xs:sequence minOccurs="3" maxOccurs="3">
<xs:element name="bar" type="barType"/>
<xs:element name="foo" type="fooType" maxOccurs="unbounded">
Program Manager, XML Technologies
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
"Mr Flibble" <mr.flibble (AT) flibbyly (DOT) wobbly.n.et> wrote
Re: Can a schema be written to check for this? - 06-19-2006 , 11:25 AM
* Stan Kitsis [MSFT] wrote:
|Thread Tools||Search this Thread|