473,491 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XML Key Validation

I am trying to get an XML schema to validate unique key name attributes, and
have been unsuccessful. Can anyone show me what I am doing wrong?

Here is the schema:

<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="bar_key">
<xsd:selector xpath=".//bar"/>
<xsd:field xpath="@name"/>
</xsd:key>
</xsd:element>

<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>

-----------------------

Here is the XML file that should report being invalid:

<?xml version="1.0" encoding="UTF-8"?>
<foo
xmlns="http://foobartest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://foobartest foo.xsd">

<bar name="One"/>
<bar name="Two"/>
<bar name="Three"/>
<bar name="One"/>
</foo>

----------------------------

The XML file should fail, but does not. Can anyone show me what is
incorrect in my schema?

Thank you for your help.

Jeff
Jul 20 '05 #1
4 1407


Jeff Hosler wrote:
I am trying to get an XML schema to validate unique key name attributes, and
have been unsuccessful. Can anyone show me what I am doing wrong?

Here is the schema:

<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="bar_key">
<xsd:selector xpath=".//bar"/>
<xsd:field xpath="@name"/>
</xsd:key>
</xsd:element>

<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>

-----------------------

Here is the XML file that should report being invalid:

<?xml version="1.0" encoding="UTF-8"?>
<foo
xmlns="http://foobartest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://foobartest foo.xsd">

<bar name="One"/>
<bar name="Two"/>
<bar name="Three"/>
<bar name="One"/>
</foo>

----------------------------

The XML file should fail, but does not. Can anyone show me what is
incorrect in my schema?


As you use a target namespace your XPath expression needs a prefix bound
to that namespace. There is also the unique element which seems more
appropriate than a key here:

<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:pf1="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="unique-name">
<xsd:selector xpath="pf1:bar"/>
<xsd:field xpath="@name"/>
</xsd:unique>
</xsd:element>

<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
Thank you so much! This problem was driving me nuts.

Since you indicate that I should have used the "unique" identifier in this
case, what case might I use the "key" identifier?

Jeff

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-online.net...


Jeff Hosler wrote:
I am trying to get an XML schema to validate unique key name attributes,
and have been unsuccessful. Can anyone show me what I am doing wrong?

Here is the schema:

<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="bar_key">
<xsd:selector xpath=".//bar"/>
<xsd:field xpath="@name"/>
</xsd:key>
</xsd:element>

<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>

-----------------------

Here is the XML file that should report being invalid:

<?xml version="1.0" encoding="UTF-8"?>
<foo
xmlns="http://foobartest"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://foobartest foo.xsd">

<bar name="One"/>
<bar name="Two"/>
<bar name="Three"/>
<bar name="One"/>
</foo>

----------------------------

The XML file should fail, but does not. Can anyone show me what is
incorrect in my schema?


As you use a target namespace your XPath expression needs a prefix bound
to that namespace. There is also the unique element which seems more
appropriate than a key here:

<xsd:schema
targetNamespace="http://foobartest"
xmlns="http://foobartest"
xmlns:pf1="http://foobartest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:element name="foo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="bar" type="barType" minOccurs="1"
maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="unique-name">
<xsd:selector xpath="pf1:bar"/>
<xsd:field xpath="@name"/>
</xsd:unique>
</xsd:element>

<xsd:complexType name="barType">
<xsd:attribute name="name" type="xsd:string" use="required"/>
</xsd:complexType>

</xsd:schema>

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #3


Jeff Hosler wrote:

Since you indicate that I should have used the "unique" identifier in this
case, what case might I use the "key" identifier?


If you elsewhere in the schema respectively the instance XML want to
have a keyref to reference keys.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #4
Thanks Martin. You've been a huge help.

Cheers,

Jeff

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-online.net...


Jeff Hosler wrote:

Since you indicate that I should have used the "unique" identifier in
this case, what case might I use the "key" identifier?


If you elsewhere in the schema respectively the instance XML want to have
a keyref to reference keys.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #5

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

Similar topics

21
3877
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email...
2
2130
by: wumingshi | last post by:
Hi, When validating an XML instance, sometimes the schema is not enough to expression the validation rules. Additional validation rules may be expressed in an application-specific way. For...
4
2586
by: Tim Meagher | last post by:
I am trying to use both validation controls and to add submit button attributes, but when I add the button attributes, the javascript fpr the validation controls is no longer created for the page. ...
14
6266
by: Matt | last post by:
I want to know if ASP.NET Web Forms Validation Controls are Server-Side or Client-Side form validation? Since I think each validator control can select either 1) JavaScript based error dialog or 2)...
6
2395
by: Stephen | last post by:
Hi, the validation controls dont work on Netscape or Mozilla and only on Internet Explorer why? How do i correct this problem? Thanks
7
2030
by: Ryan Ternier | last post by:
We're running a site that has required field validation on the login page. It works fine on our development / test machines. However, when I upload this site to our live server i get this error. ...
5
3219
by: Chris | last post by:
Based upon some prevoius postings on what to do for adding a 'add' row to a datagrid I utilize the footer to create the 'add' row. The only issue is that I have it sharing the 'UpDate_Command' and...
4
2959
by: David Colliver | last post by:
Hi all, I am having a slight problem that hopefully, someone can help me fix. I have a form on a page. Many items on the form have validation controls attached. Also on this form are...
2
4492
by: dustbort | last post by:
I recently had a problem where my required field validator stopped working. But, the page still posted back and tried to insert a record into the database without performing server-side validation....
6
3334
by: Jon Paal | last post by:
validation doesn't fire what's missing ????? /////// ---- code -----/////////////////////////// Sub btnSubmit_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) 'Handles...
0
6978
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...
1
6858
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
7360
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...
0
5451
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,...
1
4881
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...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
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 ...
0
280
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...

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.