query regarding xs:key & key ref

S

shyam

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
 
M

Martin Honnen

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

shyam

Martin said:
shyam wrote:



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

Even with this its dosent work
 
S

Stan Kitsis [MSFT]

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 said:
Martin said:
shyam wrote:



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

Even with this its dosent work
 
S

shyam

Stan said:
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
 
S

Stan Kitsis [MSFT]

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
 
S

shyam

Stan said:
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>

===============================================================
 
S

shyam

Stan said:
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>

===============================================================
 
P

Priscilla Walmsley

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top