473,395 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can a schema be written to check for this?


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?
Jun 15 '06 #1
3 1338


Mr Flibble wrote:
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/
Jun 15 '06 #2
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********@flibbyly.wobbly.n.et> wrote in message
news:e6***********@custnews.inweb.co.uk...

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?

Jun 16 '06 #3
* Stan Kitsis [MSFT] wrote:
Yes, you can do this using uniqueness constraints.


Thank you , that worked a TREAT!
Jun 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Heejeong Lee | last post by:
I am going to begin following project Description of the project: One of the proposed ways of storing XML is to "shred" the structure of the XML document and insert it into a relational...
3
by: Shailendra Batham | last post by:
hi guys I need your suggestions / opinion for doing this the right way. I have a XML and a Schema for the same What I want is when its validated against the schema, it should give custom...
7
by: Robert Stearns | last post by:
I ran the following bit of SQL and my PRIMARY KEY wound up in schema SYSIBM called SQL.... not schema is3 called primary. The index registation did wind up there. Obviously there's something I...
1
by: aj | last post by:
DB2 v8 FP5 Red Hat AS 2.1 We are encountering a strange error where the default schema name that is specified w/ a SET SCHEMA command is being inconsistently ignored, causing all table...
4
by: craig.wagner | last post by:
I have a situation where I need to carry additional information in an XML Schema. What I've found to appear to work is doing something like the following: <xs:schema xmlns:xs="...
5
by: Lemon Tree | last post by:
Hello everybody. I have a problem with Schema Definitions and I cannot figure out how to solve it (provided that it could be solved :)) Here we go... Let's suppose to have a simple XML file...
5
by: j.tremlett | last post by:
Hi, I have read various articles on this and I have had a few suggestions but would like to know which is correct. I have an XML file with the element <root> Within <root> I can have a...
4
by: Stan R. | last post by:
Hello, I have xmllint and xsltproc installed and running on both my linux and win32 platforms, all seems good. I have a couple questions though. 1) If I have an external dtd file, which is...
5
by: Sek | last post by:
Hi Folks, I would like to create a XML file that adhere to the Schema definition files. My schema definition files import/include further XSD files and internal references can be to a depth...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.