W3C home > Mailing lists > Public > xmlschema-dev@w3.org > November 2008

Keys and Scope

From: Matt Knowles <MattK@LacksValley.com>
Date: Mon, 10 Nov 2008 11:08:08 -0600
To: "xmlschema-dev@w3.org" <xmlschema-dev@w3.org>
Message-ID: <3D4031F6D3565044A4D0FC00A33C96860481A296F2@mail1.lacksvalley.com>

Hi everyone,

I'm new to XML and keep getting the feeling that everything is upside-down from my normal understanding of scope.

I have a Schema defined:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema2"
        <xs:element name="Section" type="s:SectionType">
                <xs:key name="DefinitionKey">
                        <xs:selector xpath="s:Define"/>
                        <xs:field xpath="@name"/>
                <xs:keyref name="DefinitionRef" refer="s:DefinitionKey">
                        <xs:selector xpath="s:Refer"/>
                        <xs:field xpath="@ref"/>
        <xs:complexType name="SectionType" mixed="true">
                <xs:sequence minOccurs="1" maxOccurs="1">
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="Define" type="s:DefineType"/>
                        <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="Refer" type="s:ReferType"/>
                                <xs:element ref="s:Section"/>
        <xs:complexType name="DefineType">
                        <xs:extension base="s:SectionType">
                                <xs:attribute name="name" type="xs:string"/>
        <xs:complexType name="ReferType">
                        <xs:extension base="s:SectionType">
                                <xs:attribute name="ref" type="xs:string"/>

I have the following instance document:

<?xml version="1.0" encoding="utf-8" ?>
<Section xmlns="http://tempuri.org/XMLSchema2.xsd">
        <Define name="one"/>
        <Refer ref="one"/> <!-- Allow: defined in current node. -->
        <Refer ref="two"/> <!-- Error: not defined in current or ancestor node. -->
                <Define name="two"/>
                <Refer ref="one"/> <!-- Allow: defined in ancestor node. -->
                <Refer ref="two"/> <!-- Allow: defined in current node. -->

I would like to define the keys and keyrefs in the schema such that I can enforce the rules in the comments above, but I'm not finding a way to do this.

Basically, I'm thinking of a node and all it's child nodes as my scope, and would like to Refer to anything Defined in the same scope, but disallow any References to things Defined out of scope.

The closest I've been able to come is the reverse, where I can Refer to anything Defined in the current or any descendant node, by changing:

                <xs:key name="DefinitionKey">
                        <xs:selector xpath=".//s:Define"/>
                        <xs:field xpath="@name"/>

Am I hopelessly stuck in a mindset that just doesn't work with XML?  Or is there an obvious solution I'm just overlooking?

Any help would be greatly appreciated.

Matt Knowles
Received on Tuesday, 11 November 2008 15:26:14 UTC

This archive was generated by hypermail 2.4.0 : Friday, 17 January 2020 23:15:48 UTC