473,804 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1354


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:simpleTyp e name="animalTyp e">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="dog"/>
<xs:enumerati on value="cat"/>
<xs:enumerati on value="fish"/>
</xs:restriction>
</xs:simpleType>

<xs:complexTy pe name="barType">
<xs:simpleConte nt>
<xs:extension base="xs:string ">
<xs:attribute name="x" type="animalTyp e"/>
<xs:attribute name="colour" type="xs:string "/>
</xs:extension>
</xs:simpleConten t>
</xs:complexType>

<xs:complexTy pe 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="unbo unded">
<xs:unique name="uniqueAni mal">
<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********@fli bbyly.wobbly.n. et> wrote in message
news:e6******** ***@custnews.in web.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
3278
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 DBMS. To do this, I will consider the query workload over the XML document. The purpose of this project is to
3
6061
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 errors saying what happened and where...... is it possible to do that ? for eg. if the Attribute is missing or invalid in the following element <Watch ID="999d999">
7
7386
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 don't understand or see. If you have some insight into this problem that you are willing to share, I'd be grateful. One possibility is the uniqueness required: is the constraint required to be unique with table, schema or database? PS: Ignore the...
1
3872
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 references to be prefixed with the connecting username, and throwing many SQL -204 SQLSTATE 42704 exceptions. There does not appear to be a pattern to this, except that heavy
4
1938
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=" http://www.w3.org/2001/XMLSchema" xmlns:xx="http://www.mycompany.com"> <xs:complexType name="SomeType"> <xs:sequence> <xs:element name="AccountNumber" type="xs:int" xx:errorAction="fail"/>
5
1333
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 with the following tags: <type id="id1"/> <type id="id2"/> <type id="id3"/>
5
3418
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 number of child elements occuring in any order and any number of times.
4
7747
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 included in my xml file, like <!DOCTYPE userlist SYSTEM "test.dtd">
5
2281
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 of two or three. The data required for the creation of XML are present in the form of user defined data structures.
0
9708
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.