keyref validation problems

Discussion in 'XML' started by jasonnerothin@gmail.com, Apr 27, 2006.

  1. Guest

    I'm trying to figure out exactly what's going wrong with an xpath
    keyref validation scenario.

    In brief, I have something like the following in my xsd:
    <xs:schema>
    <xs:complexType name="topLevelContainer">

    <!-- define an element -->
    <xs:complexType name="element">
    ...
    <xs:attribute name="id" type="xs:int" use="required"/>
    </xs:complexType>

    <!-- instance one, simple list -->
    <xs:element name="instanceOne" minOccurs="0">
    <xs:sequecne>
    <xs:element name="elementDescription" type="element"
    minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
    </xs:complexType>

    <!-- instance two, an inner, heterogenous list -->
    <xs:complexType name="instanceTwo" type="anotherType">
    <xs:element name="elementList">
    <xs:complexType>
    <xs:choice maxOccurs="unbounded">
    <xs:complexType name="fullElement"
    type="elementType"/>
    <xs:complexType name="elementRef" type="xs:int"/>
    </xs:choice>
    </xs:complexType>
    </xs:sequence>

    <!-- constraints -->
    <!-- key points to the id attribute of instanceOne type -->
    <xs:key name="elementKey">
    <xs:selector xpath="instanceOne/elementDescription"/>
    <xs:field xpath="@id">
    </xs:key>

    <!-- keyref points to more-deeply nested instanceTwo type -->
    <xs:keyref name="elementRef" refer="elementKey">
    <xs:selector xpath="instanceTwo/elementList/elementRef"/>
    <xs:field xpath="."/>
    </xs:keyref>

    </xs:complexType> <!-- end top-level container -->
    </xs:schema>

    In short, what I am trying to do is ensure that the references from
    within the instanceTwo type point to elements within the instanceOne
    type. But what I'm not able to figure out is how to enforce that the
    instanceTwo type contain references to fully described elements in
    instanceOne only. For some reason, a file will validate when the
    references out of instanceTwo refer to elementDescriptions from other
    instanceTwos in the file.

    I'm suspicious that the xpath comparison will never return false for
    some reason related to datatype. (My xml editor seems to suggest that
    the xpath expressions are returning the appropriate values.)

    ???
    , Apr 27, 2006
    #1
    1. Advertising

  2. Hi,

    Your schema has quite a large number of structural problems - keys
    within complex types instead of elements, complex types with type
    attributes, complex types directly within choice groups, etc.

    I'm assuming this is because you cut down the example to be concise, but
    it makes it awfully hard to debug the problem when it's not a valid
    schema.

    Your general approach looks right to me. It could be a namespace issue
    - if you're using a target namespace, you must prefix all your element
    names in your XPaths. It could also be a problem with your XML Editor,
    since some of them are buggy when it comes to identity constraints.

    Otherwise, you could post the real schema and we could take a look at
    it.

    Hope that helps,
    Priscilla

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

    *** Sent via Developersdex http://www.developersdex.com ***
    Priscilla Walmsley, Apr 27, 2006
    #2
    1. Advertising

  3. BioInfoGuy Guest

    BioInfoGuy, Apr 27, 2006
    #3
  4. Hi,

    The most obvious thing I notice is that you are using a target
    namespace, but you are not prefixing the element names in your paths.
    XPath expressions are not affected by default namespace declarations, so
    you will need to declare a prefix for the default namespace, e.g.

    xmlns:xyz="net:sf:psidev:mi"

    Then prefix all the element names in your XPaths, e.g.

    <xs:selector xpath="xyz:experimentList/xyz:experimentDescription" />

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

    *** Sent via Developersdex http://www.developersdex.com ***
    Priscilla Walmsley, Apr 28, 2006
    #4
  5. BioInfoGuy Guest

    Hi Priscilla,

    Perhaps you could suggest a good xml style guide or a tool better than
    those that I am using? Both XmlSpy and a java validator I wrote myself
    (sax parser) fail to choke on the problem use case that I have on hand.

    Thanks again...
    Jason
    BioInfoGuy, May 1, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marek Malowidzki

    Key/keyref validation in schema?

    Marek Malowidzki, Aug 18, 2003, in forum: XML
    Replies:
    1
    Views:
    508
    Mark Preston
    Aug 19, 2003
  2. J E E
    Replies:
    0
    Views:
    377
    J E E
    Nov 7, 2003
  3. peterpeter
    Replies:
    0
    Views:
    927
    peterpeter
    Feb 11, 2004
  4. Markus Seibold

    <key><keyref> does not work in XMLSpy

    Markus Seibold, Feb 13, 2004, in forum: XML
    Replies:
    0
    Views:
    1,309
    Markus Seibold
    Feb 13, 2004
  5. peterpeter
    Replies:
    0
    Views:
    734
    peterpeter
    Feb 12, 2007
Loading...

Share This Page