XPath on xs:selector for a xs:keyref  
Author Message
Andy Lichey





PostPosted: XML and the .NET Framework, XPath on xs:selector for a xs:keyref Top

Thank you in advance for any help you can give...

I have a problem. What I'd like to do is define a relationship in an XSD between two tables, where the relationship returns the last row in the child table by default. I thought that the following should work:

<xs:keyref name="project_taskproject_eventlast" refer="project_taskkey1" msdata:DeleteRule="Cascade" msdata:UpdateRule="Cascade">
<xs:selector xpath=".//project_event[last()]"/>
<xs:field xpath="proj_task_id"/>
</xs:keyref>

The problem is that the .Net compiler for this returns an error saying that the xpath found in the selector is invalid.

Now, I tried helping myself by plowing through the W3C specifications to see if this was even allowed, and though I found references to the xpath being allowed for a selector only being a subset of all of valid xpath, I couldn't find a specific reference to what was, and was not allowed.

Is there anyway for me to do this from the XSD itself Thanks again!



.NET Development31  
 
 
Stan Kitsis - MSFT





PostPosted: XML and the .NET Framework, XPath on xs:selector for a xs:keyref Top

Hi Andy,

Xml Schema indeed only allows a subset of the XPath to be used in the field and selector elements. The formal definition can be found here: http://www.w3.org/TR/xmlschema-1/#coss-identity-constraint

Stan Kitsis



 
 
marco.ragogna





PostPosted: XML and the .NET Framework, XPath on xs:selector for a xs:keyref Top

I tried to understand the specifications but sometimes is not easy.
I cannot find where it is defined the fact the XPath selector expression cannot be something like:

selector=".//generic-element"

 
 
Martin Honnen





PostPosted: XML and the .NET Framework, XPath on xs:selector for a xs:keyref Top

I think .//element-name is possible but the original poster in this thread had a predicate behind that (e.g. [last()]) and while XPath in general allows predicates to filter out nodes the XPath subset defined for the XSD schema language does not allow predicates.

 
 
marco.ragogna





PostPosted: XML and the .NET Framework, XPath on xs:selector for a xs:keyref Top

I would like to continue this thread because I really need to understand the specification of key/keyrefs and their selectors.

I need to create in some way a recursive mechanism. I am posting you a short schema example:

< xml version="1.0" encoding="UTF-8" >

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Book" type="Book_CT">
<xs:key name="ChapterKey">
<xs:selector xpath="Chapter"/>
<xs:field xpath "/>
</xs:key>
<xs:keyref name="ChapterKeyRef" refer="ChapterKey">
<xs:selector xpath="TOC"/>
<xs:field xpath="Chapter"/>
</xs:keyref>
</xs:element>
<xs:complexType name="Book_CT">
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
<xs:element name="TOC" type="TOC_CT"/>
<xs:element name="Chapter" type="Chapter_CT"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Chapter_CT">
<xs:sequence minOccurs="0">
<xs:element name="Chapter" type="Chapter_CT" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="TOC_CT">
<xs:sequence>
<xs:element name="Chapter" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</
xs:schema>

And the correspondent XML project

<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="..\..\Desktop\Untitled1.xsd">
<Title/>
<Author/>
<TOC>
<Chapter>Chapter 1</Chapter>
</TOC>
<Chapter Name="Chapter 1">
    <Chapter Name="Chapter 2.1"/>
    <Chapter Name="Chapter 2.2">
        <Chapter Name="Chapter 2.2.1"/>
     </Chapter>
</Chapter>
</
Book>

This example works, but not in the way I like.

The problem is that the definition

<xs:key name="ChapterKey">

<xs:selector xpath="//Chapter"/>

<xs:field xpath "/>

</xs:key>

is not accepted by Altova. Is a problem of Altova, or the XML Schema specification do not allow this