473,397 Members | 1,949 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,397 software developers and data experts.

XML Schema Definitions

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"/>

Now I would like to express, by using an XSD, the following restriction
on a tag attribute: the value of the attribute must be the value of the
id attribute of some previously defined <type> tag.

For example, let's suppose to have a <foo> tag with a "type" attribute.
Then the admissible values for the "type" attribute would be id1, id2
or id3.

At the beginning I thought it was not possible but, while I was writing
an XSD I noticed that I was doing the previously described thing all
the time!!! In fact, my XSD are full of type declarations like

<xs:complexType name="myTypeName">
...
</xs:complexType>

and these declarations are used to define the type of elements, for
example

<xs:element type="myTypeName"> ...

The validator complains if the value of the "type" attribute has not
been previously defined
That's exactly the thing I would like to do with my XML files :)

I saw the XSD of the XSD but it is too complicated and by looking at it
I cannot understand if this kind of enforcement (using declared types)
can be embedded in the XSD itself or it is part of an ad-hoc logic of
the validator program.

I still think that it is not possible to enforce such a kind of
restriction by simply using XSD files but I might be wrong. Any hint?

Thank you

Feb 13 '06 #1
5 1315


Lemon Tree wrote:

Let's suppose to have a simple XML file with the following tags:

<type id="id1"/>
<type id="id2"/>
<type id="id3"/>

Now I would like to express, by using an XSD, the following restriction
on a tag attribute: the value of the attribute must be the value of the
id attribute of some previously defined <type> tag.

For example, let's suppose to have a <foo> tag with a "type" attribute.
Then the admissible values for the "type" attribute would be id1, id2
or id3.


Look into xs:key and xs:keyref, you define those id attributes as keys
on the type element, then you define a keyref for the foo element so
that the type attribute on foo is a key reference.

Example and introduction is here:
<http://www.w3.org/TR/xmlschema-0/#specifyingKeysAndtheirRefs>
<http://www.w3.org/TR/xmlschema-0/#report.xsd>
If those id attributes are unique in the complete XML document then you
could also use xs:ID as the id attribute type for your type element and
xs:IDREF for the foo attributes.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 13 '06 #2

Martin Honnen ha scritto:
Example and introduction is here:
<http://www.w3.org/TR/xmlschema-0/#specifyingKeysAndtheirRefs>
<http://www.w3.org/TR/xmlschema-0/#report.xsd>
If those id attributes are unique in the complete XML document then you
could also use xs:ID as the id attribute type for your type element and
xs:IDREF for the foo attributes.

Thank you for you help.
I will profit a bit of this thread to ask another thing... I was
experimenting with what it is written in the document cited before. In
particular I was trying to use the <unique> tag.

Now I am using Xerces to validate the XML against the schema, but it
doesn't work.
I cannot tell if it is an error in the Schema or if Xerces is unable to
check this kind of constraints.

Here it is the XSD and the XML:

* XSD
************************************************** **************************************************

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xnemesis.org"
xmlns="http://www.xnemesis.org"
elementFormDefault="qualified">

<xs:complexType name="resourceType">
<xs:attribute name="id" type="xs:string"/>
<xs:attribute name="type" type="xs:string"/>
</xs:complexType>

<xs:element name="declarations">
<xs:complexType>
<xs:sequence>
<xs:element name="resource" type="resourceType"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:unique name="dummy">
<xs:selector xpath="resource"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>

</xs:schema>

* XML
************************************************** ************************************************** ******

<?xml version="1.0"?>
<declarations xmlns="http://www.xnemesis.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xnemesis.org
declaration.xsd">

<resource id="id" type="type"/>
<resource id="id" type="type"/>
<resource id="id" type="type"/>
<resource id="id" type="type"/>

</declarations>
************************************************** ************************************************** *******

I cannot see anything wrong with the previous files but the XML is
recognized as valid while the <unique> element in the XSD should
prevent the declaration of many <resource>s with the same id.

Any hint?
Thank you

Feb 13 '06 #3
The schema constraint looks for resource elements from no namespace
while you have in your document resource elements from the
http://www.xnemesis.org namespace. Note that the XPath subset from the
XML Schema as well as XPath 1.0 has no notion of default namespace,
name tests without a prefix are considered to be from no namespace. You
should change your schema as below:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xnemesis.org"
xmlns="http://www.xnemesis.org"
xmlns:t="http://www.xnemesis.org"
elementFormDefault="qualified">

<xs:complexType name="resourceType">
<xs:attribute name="id" type="xs:string"/>
<xs:attribute name="type" type="xs:string"/>
</xs:complexType>

<xs:element name="declarations">
<xs:complexType>
<xs:sequence>
<xs:element name="resource" type="resourceType"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:unique name="dummy">
<xs:selector xpath="t:resource"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>

</xs:schema>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Feb 13 '06 #4


Lemon Tree wrote:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xnemesis.org"
xmlns="http://www.xnemesis.org"
Add
xmlns:xn="http://www.xnemesis.org"
here
elementFormDefault="qualified">
<xs:element name="declarations">
<xs:complexType>
<xs:sequence>
<xs:element name="resource" type="resourceType"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:unique name="dummy">
<xs:selector xpath="resource"/>


Then you need e.g.
<xs:selector xpath="xn:resource" />

as your elements are in a namespace and XPath nodes tests like resource
select/match elements in no namespace whereas with xn:resource and the
above namespace declaration the XPath selects/matches resource elements
in the namespace with the URI http://www.xnemesis.org. You don't have to
change anything in the XML instance documents you want to validate.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 13 '06 #5
I thank you all for your helpful suggestions.
Now it works perfectly!

Thank you again

Feb 13 '06 #6

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

Similar topics

2
by: wooks | last post by:
I apologise if this has been asked before, it is a bit hard to search for. I have an xml schema which defines the elements in the namespace "urn:dummy". The schema does not contain any...
1
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm designing my first XML-based application and am stuck on an issue that I presume is pretty simple. I'm trying to write a schema that can be used to validate my XML file. ...
0
by: kyancy | last post by:
Hello All. We have several XML schemas to describe common component document parts. We then create new XML schemas as necessary that use "xsd:import schemaLocation=whateverLocation.." to include...
4
by: stiank81 | last post by:
Hi. I have a problem witch I assume there is an answear to....? I have several XML schemas, and they all have quiet a lot of definitions in common. They still have to be seperated into...
2
by: Vitali Gontsharuk | last post by:
Hi! I can't find an answer to the following question: does the "import" command in XML Schema import also element declarations? Or only type definitions? As far as I understood, the first takes...
3
by: Davide Bedin | last post by:
I have a "library" schema with the simple and complex types I commonly use in other schemas and then several other schemas, maybe created by other developers, that import/include the library...
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: billa1972 | last post by:
Hi, I am trying to hook into Yellow Freight's rating webservice. Below is the wsdl. When i try and create a proxy file with wsdl.exe i get the following errors, see below. Also, when i...
6
by: Rory Campbell-Lange | last post by:
I think this is a suggestion/comment! pg_dump man page: --schema-only Dump only the schema (data definitions), no data I think this use of the word schema is confusing, meaning data...
5
by: Jeff | last post by:
We are using .Net and the wsdl Utility to generate proxies to consume web services built using the BEA toolset. The data architects on the BEA side create XML schemas with various entities in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.