469,609 Members | 1,179 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

query regarding xs:key & key ref

Hi All

As per my understanding we use xs:key when we want to have a unique
value for a element or attribute and keyref to refer to one of the
unique values of that element or attribute.

However I am not sure if this uniqueness is checked when we validate a
document using XMLSpy

Here is the protion of myschema

================================================== ================
<xs:element name="PROTOHEADERENTRY">
<xs:complexType>
<xs:sequence>
<xs:element name="ABNFGRAMMER">
<xs:complexType>
<xs:sequence>
<xs:element name="ABNFRULESET">
<xs:simpleType>
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="NAME" type="xs:token"/>
<xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
================================================== ===============

now at the root element , which is above PROTOHEADERENTRY, i do this
================================================== ==============
<xs:key name="KEYHEADERNUM">
<xs:selector xpath="./PROTOHEADERENTRY"/>
<xs:field xpath="HEADERSEQNUM"/>
</xs:key>

Now when I write a xml file and validate against this schema it gets
validated even if I use same HEADERSEQNUM value for multiple
PROTOHEADERENTRY elements

I am using XMLSpy

I think I am missing some trick here

PLease Help

Regards
Shyam

Jun 16 '06 #1
8 1192


shyam wrote:

<xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
<xs:selector xpath="./PROTOHEADERENTRY"/>
<xs:field xpath="HEADERSEQNUM"/>


XPath to select an attribute is @attributeName so you probably want
<xs:field xpath="@HEADERSEQNUM"/>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 16 '06 #2

Martin Honnen wrote:
shyam wrote:

<xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
<xs:selector xpath="./PROTOHEADERENTRY"/>
<xs:field xpath="HEADERSEQNUM"/>


XPath to select an attribute is @attributeName so you probably want
<xs:field xpath="@HEADERSEQNUM"/>


Even with this its dosent work

--

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


Jun 16 '06 #3
You need to declare a target namespace in your schema and use it in the
xpath. In the example below I used myTNS as the prefix for your target
namespace.

<xs:schema ... targetNamespace="myTargetNamespace"
xmlns:myTNS="myTargetNamespace" xmlns="myTargetNamespace"...>

.....

<xs:selector xpath="./myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@HEADERSEQNUM"/>

....

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Martin Honnen wrote:
shyam wrote:

> <xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>


> <xs:selector xpath="./PROTOHEADERENTRY"/>
> <xs:field xpath="HEADERSEQNUM"/>


XPath to select an attribute is @attributeName so you probably want
<xs:field xpath="@HEADERSEQNUM"/>


Even with this its dosent work

--

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

Jun 16 '06 #4

Stan Kitsis [MSFT] wrote:
You need to declare a target namespace in your schema and use it in the
xpath. In the example below I used myTNS as the prefix for your target
namespace.

<xs:schema ... targetNamespace="myTargetNamespace"
xmlns:myTNS="myTargetNamespace" xmlns="myTargetNamespace"...>

....

<xs:selector xpath="./myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@HEADERSEQNUM"/>

Thanks Stan This has worked ,
however the keyref thing is still not working
I have defined a keyref
<xs:keyref name="KEYENTRYNUM" refer="myTNS:KEYHEADERNUM">
<xs:selector xpath="./myTNS:PROTOHEADER"/>
<xs:field xpath="@ENTRYSEQNUM"/>
</xs:keyref>

which refers to the earlier xs:key

So i want is that the ENTRYSEQNUM attribute of PROTOHEADER element
should refer to a unique and existing HEADERSEQNUM attribute of
myTNS:PROTOHEADERENTRY element

I guess I am again missing some minor trick and would graetly
appretiate if you could help out

Thanks
Shyam
...

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Martin Honnen wrote:
shyam wrote:
> <xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
> <xs:selector xpath="./PROTOHEADERENTRY"/>
> <xs:field xpath="HEADERSEQNUM"/>

XPath to select an attribute is @attributeName so you probably want
<xs:field xpath="@HEADERSEQNUM"/>


Even with this its dosent work

--

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


Jun 17 '06 #5
Shyam,

Can you post your schema?

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...

Stan Kitsis [MSFT] wrote:
You need to declare a target namespace in your schema and use it in the
xpath. In the example below I used myTNS as the prefix for your target
namespace.

<xs:schema ... targetNamespace="myTargetNamespace"
xmlns:myTNS="myTargetNamespace" xmlns="myTargetNamespace"...>

....

<xs:selector xpath="./myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@HEADERSEQNUM"/>


Thanks Stan This has worked ,
however the keyref thing is still not working
I have defined a keyref
<xs:keyref name="KEYENTRYNUM" refer="myTNS:KEYHEADERNUM">
<xs:selector xpath="./myTNS:PROTOHEADER"/>
<xs:field xpath="@ENTRYSEQNUM"/>
</xs:keyref>

which refers to the earlier xs:key

So i want is that the ENTRYSEQNUM attribute of PROTOHEADER element
should refer to a unique and existing HEADERSEQNUM attribute of
myTNS:PROTOHEADERENTRY element

I guess I am again missing some minor trick and would graetly
appretiate if you could help out

Thanks
Shyam
...

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
>
> Martin Honnen wrote:
>> shyam wrote:
>>
>>
>> > <xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
>>
>>
>> > <xs:selector xpath="./PROTOHEADERENTRY"/>
>> > <xs:field xpath="HEADERSEQNUM"/>
>>
>> XPath to select an attribute is @attributeName so you probably want
>> <xs:field xpath="@HEADERSEQNUM"/>
>
> Even with this its dosent work
>
>>
>> --
>>
>> Martin Honnen
>> http://JavaScript.FAQTs.com/
>

Jun 19 '06 #6

Stan Kitsis [MSFT] wrote:
Shyam,

Can you post your schema?
================================================== ============
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3schools.com" xmlns:myTNS="MYTARGET"
targetNamespace="MYTARGET" elementFormDefault="qualified">
<xs:element name="PROTOHEADERENTRY">
<xs:complexType>
<xs:sequence>
<xs:element name="ABNFGRAMMER">
<xs:complexType>
<xs:sequence>
<xs:element name="ABNFRULESET">
<xs:simpleType>
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
</xs:element>
<xs:element name="ABNFEXCEPTION" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="RULENAME" type="xs:token"/>
<xs:attribute name="EXCEPTION">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="MANDATORY"/>
<xs:enumeration value="IGNORESEQ"/>
<xs:enumeration value="IGNORECASE"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="NAME" type="xs:token"/>
<xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
<xs:element name="ABNFBASIC">
<xs:complexType>
<xs:sequence>
<xs:element name="RULESTRING" type="xs:string"/>
</xs:sequence>
<xs:attribute name="ABNFBASICNAME" type="xs:NMTOKEN"/>
</xs:complexType>
</xs:element>
<xs:element name="ABNFRULE">
<xs:complexType>
<xs:sequence>
<xs:element name="RULESTRING">
<xs:simpleType>
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="RULENAME" type="xs:NMTOKEN"/>
</xs:complexType>
</xs:element>
<xs:element name="PROTOHEADER">
<xs:complexType>
<xs:attribute name="PROTOHEADERNAME" type="xs:token"/>
<xs:attribute name="ENTRYSEQNUM" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
<xs:element name="PROTODEF">
<xs:complexType>
<xs:sequence>
<xs:element name="PROTOTABLE">
<xs:complexType>
<xs:sequence>
<xs:element ref="myTNS:PROTOHEADER" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="myTNS:PROTOHEADERENTRY" maxOccurs="unbounded"/>
<xs:element ref="myTNS:ABNFRULE" maxOccurs="unbounded"/>
<xs:element ref="myTNS:ABNFBASIC" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="PROTOCOLNAME" type="xs:token"/>
<xs:attribute name="PROJECTNAME" type="xs:string"/>
</xs:complexType>
<xs:key name="KEYHEADERNUM">
<xs:selector xpath="myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@HEADERSEQNUM"/>
</xs:key>
<xs:key name="KEYHEADERNAME">
<xs:selector xpath="./myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@NAME"/>
</xs:key>
<xs:keyref name="KEYENTRYNUM" refer="myTNS:KEYHEADERNUM">
<xs:selector xpath="./myTNS:PROTOHEADER"/>
<xs:field xpath="@ENTRYSEQNUM"/>
</xs:keyref>
</xs:element>
</xs:schema>

================================================== =============

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...

Stan Kitsis [MSFT] wrote:
You need to declare a target namespace in your schema and use it in the
xpath. In the example below I used myTNS as the prefix for your target
namespace.

<xs:schema ... targetNamespace="myTargetNamespace"
xmlns:myTNS="myTargetNamespace" xmlns="myTargetNamespace"...>

....

<xs:selector xpath="./myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@HEADERSEQNUM"/>


Thanks Stan This has worked ,
however the keyref thing is still not working
I have defined a keyref
<xs:keyref name="KEYENTRYNUM" refer="myTNS:KEYHEADERNUM">
<xs:selector xpath="./myTNS:PROTOHEADER"/>
<xs:field xpath="@ENTRYSEQNUM"/>
</xs:keyref>

which refers to the earlier xs:key

So i want is that the ENTRYSEQNUM attribute of PROTOHEADER element
should refer to a unique and existing HEADERSEQNUM attribute of
myTNS:PROTOHEADERENTRY element

I guess I am again missing some minor trick and would graetly
appretiate if you could help out

Thanks
Shyam
...

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
>
> Martin Honnen wrote:
>> shyam wrote:
>>
>>
>> > <xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
>>
>>
>> > <xs:selector xpath="./PROTOHEADERENTRY"/>
>> > <xs:field xpath="HEADERSEQNUM"/>
>>
>> XPath to select an attribute is @attributeName so you probably want
>> <xs:field xpath="@HEADERSEQNUM"/>
>
> Even with this its dosent work
>
>>
>> --
>>
>> Martin Honnen
>> http://JavaScript.FAQTs.com/
>


Jun 20 '06 #7
Stan Kitsis [MSFT] wrote:
Shyam,

Can you post your schema?
================================================== ============
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.w3schools.com" xmlns:myTNS="MYTARGET"
targetNamespace="MYTARGET" elementFormDefault="qualified">
<xs:element name="PROTOHEADERENTRY">
<xs:complexType>
<xs:sequence>
<xs:element name="ABNFGRAMMER">
<xs:complexType>
<xs:sequence>
<xs:element name="ABNFRULESET">
<xs:simpleType>
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
</xs:element>
<xs:element name="ABNFEXCEPTION" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="RULENAME" type="xs:token"/>
<xs:attribute name="EXCEPTION">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="MANDATORY"/>
<xs:enumeration value="IGNORESEQ"/>
<xs:enumeration value="IGNORECASE"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="NAME" type="xs:token"/>
<xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
<xs:element name="ABNFBASIC">
<xs:complexType>
<xs:sequence>
<xs:element name="RULESTRING" type="xs:string"/>
</xs:sequence>
<xs:attribute name="ABNFBASICNAME" type="xs:NMTOKEN"/>
</xs:complexType>
</xs:element>
<xs:element name="ABNFRULE">
<xs:complexType>
<xs:sequence>
<xs:element name="RULESTRING">
<xs:simpleType>
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="RULENAME" type="xs:NMTOKEN"/>
</xs:complexType>
</xs:element>
<xs:element name="PROTOHEADER">
<xs:complexType>
<xs:attribute name="PROTOHEADERNAME" type="xs:token"/>
<xs:attribute name="ENTRYSEQNUM" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
<xs:element name="PROTODEF">
<xs:complexType>
<xs:sequence>
<xs:element name="PROTOTABLE">
<xs:complexType>
<xs:sequence>
<xs:element ref="myTNS:PROTOHEADER" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element ref="myTNS:PROTOHEADERENTRY" maxOccurs="unbounded"/>
<xs:element ref="myTNS:ABNFRULE" maxOccurs="unbounded"/>
<xs:element ref="myTNS:ABNFBASIC" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="PROTOCOLNAME" type="xs:token"/>
<xs:attribute name="PROJECTNAME" type="xs:string"/>
</xs:complexType>
<xs:key name="KEYHEADERNUM">
<xs:selector xpath="myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@HEADERSEQNUM"/>
</xs:key>
<xs:key name="KEYHEADERNAME">
<xs:selector xpath="./myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@NAME"/>
</xs:key>
<xs:keyref name="KEYENTRYNUM" refer="myTNS:KEYHEADERNUM">
<xs:selector xpath="./myTNS:PROTOHEADER"/>
<xs:field xpath="@ENTRYSEQNUM"/>
</xs:keyref>
</xs:element>
</xs:schema>

================================================== =============

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...

Stan Kitsis [MSFT] wrote:
You need to declare a target namespace in your schema and use it in the
xpath. In the example below I used myTNS as the prefix for your target
namespace.

<xs:schema ... targetNamespace="myTargetNamespace"
xmlns:myTNS="myTargetNamespace" xmlns="myTargetNamespace"...>

....

<xs:selector xpath="./myTNS:PROTOHEADERENTRY"/>
<xs:field xpath="@HEADERSEQNUM"/>


Thanks Stan This has worked ,
however the keyref thing is still not working
I have defined a keyref
<xs:keyref name="KEYENTRYNUM" refer="myTNS:KEYHEADERNUM">
<xs:selector xpath="./myTNS:PROTOHEADER"/>
<xs:field xpath="@ENTRYSEQNUM"/>
</xs:keyref>

which refers to the earlier xs:key

So i want is that the ENTRYSEQNUM attribute of PROTOHEADER element
should refer to a unique and existing HEADERSEQNUM attribute of
myTNS:PROTOHEADERENTRY element

I guess I am again missing some minor trick and would graetly
appretiate if you could help out

Thanks
Shyam
...

--
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
"shyam" <sh********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
>
> Martin Honnen wrote:
>> shyam wrote:
>>
>>
>> > <xs:attribute name="HEADERSEQNUM" type="xs:positiveInteger"/>
>>
>>
>> > <xs:selector xpath="./PROTOHEADERENTRY"/>
>> > <xs:field xpath="HEADERSEQNUM"/>
>>
>> XPath to select an attribute is @attributeName so you probably want
>> <xs:field xpath="@HEADERSEQNUM"/>
>
> Even with this its dosent work
>
>>
>> --
>>
>> Martin Honnen
>> http://JavaScript.FAQTs.com/
>


Jun 20 '06 #8
Hi,

From your schema, it looks like PROTOHEADER is a grandchild of PROTODEF,
so you should use TWO slashes in your keyref selector, as in:

.//myTNS:PROTOHEADER

or you could use a more precise path:

myTNS:PROTOTABLE/myTNS:PROTOHEADER

Hope that helps,
Priscilla
----------------------------------
Priscilla Walmsley
Author, Definitive XML Schema
XQuery
http://www.datypic.com
----------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Jun 21 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bruce | last post: by
1 post views Thread by SC | last post: by
reply views Thread by daldridge | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.