Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 09:39 AM
Jeff Hosler
Guest
 
Posts: n/a
Default 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


  #2  
Old July 20th, 2005, 09:39 AM
Martin Honnen
Guest
 
Posts: n/a
Default Re: XML Key Validation



Jeff Hosler wrote:
[color=blue]
> 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?[/color]

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/
  #3  
Old July 20th, 2005, 09:39 AM
Jeff Hosler
Guest
 
Posts: n/a
Default Re: XML Key Validation

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" <mahotrash@yahoo.de> wrote in message
news:4291c480$0$14737$9b4e6d93@newsread4.arcor-online.net...[color=blue]
>
>
> Jeff Hosler wrote:
>[color=green]
>> 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?[/color]
>
> 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/[/color]


  #4  
Old July 20th, 2005, 09:40 AM
Martin Honnen
Guest
 
Posts: n/a
Default Re: XML Key Validation



Jeff Hosler wrote:

[color=blue]
> Since you indicate that I should have used the "unique" identifier in this
> case, what case might I use the "key" identifier?[/color]

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

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #5  
Old July 20th, 2005, 09:40 AM
Jeff Hosler
Guest
 
Posts: n/a
Default Re: XML Key Validation

Thanks Martin. You've been a huge help.

Cheers,

Jeff

"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:42930842$0$14729$9b4e6d93@newsread4.arcor-online.net...[color=blue]
>
>
> Jeff Hosler wrote:
>
>[color=green]
>> Since you indicate that I should have used the "unique" identifier in
>> this case, what case might I use the "key" identifier?[/color]
>
> If you elsewhere in the schema respectively the instance XML want to have
> a keyref to reference keys.
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/[/color]


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles