问题描述:

I have an important issue with XSD, XML, XPath and so on.

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>

<biblioteka xmlns:aw="http://www.w3.org/2001/XMLSchema-instance" aw:noNamespaceSchemaLocation="schema.xsd">

<gatunek>

<identyfikator id="fan"/>

</gatunek>

<dział>

<książka gatunek_id="fan"/>

</dział>

</biblioteka>

And this is schema.xsd :

<?xml version="1.0" encoding="utf-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="biblioteka" type="moja_biblioteka">

<xs:unique name="IDUnique">

<xs:selector xpath="gatunek/identyfikator"/>

<xs:field xpath="@id"/>

</xs:unique>

<xs:keyref name="gatunekIDKeyRef" refer="IDUnique">

<xs:selector xpath="dział/książka"/>

<xs:field xpath="@gatunek_id" />

</xs:keyref>

</xs:element>

<xs:complexType name="moja_biblioteka">

<xs:sequence>

<xs:element ref="gatunek"/>

<xs:element ref="dział" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

<xs:element name="gatunek">

<xs:complexType>

<xs:sequence>

<xs:element name="identyfikator" minOccurs="1" maxOccurs="unbounded">

<xs:complexType>

<xs:simpleContent>

<xs:extension base="xs:string">

<xs:attribute name="id" use="required"/>

</xs:extension>

</xs:simpleContent>

</xs:complexType>

</xs:element>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="dział">

<xs:complexType>

<xs:sequence>

<xs:element name="książka" maxOccurs="unbounded"/>

</xs:sequence>

</xs:complexType>

</xs:element>

<xs:element name="książka">

<xs:complexType>

<xs:attribute name="gatunek_id" use="required">

<xs:simpleType>

<xs:restriction base="xs:string">

<xs:pattern value="[a-z]{3,6}"/>

</xs:restriction>

</xs:simpleType>

</xs:attribute>

</xs:complexType>

</xs:element>

</xs:schema>

All seems to be fine, but it's not :/. When I validate this (I use NetBeans) I get this error message:

Key 'gatunekIDKeyRef' with value 'null' not found for identity constraint of element 'biblioteka'. [10]

I fighting with these for hours, others solutions I found of stackoverflow are not helpful for me.

网友答案:

There appear to be two errors involved here. Your error is declaring a top-level element named książka but then not referring to it from the type for dział, and instead declaring a local element with the same name.

When I validate your sample document against your sample schema using Saxon, I get the warning

Local element declaration matches the name of a global element, and allows any content. Did you mean to write ref='książka' instead of name='książka'? To suppress this warning, add type='xs:anyType'

Saxon reports, however, that the document is valid.

When I change the declaration for dział to

<xs:element name="dział">
  <xs:complexType>
    <xs:sequence>
      <!--<xs:element name="książka" maxOccurs="unbounded"/>-->
      <xs:element ref="książka" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>       
</xs:element>

both Saxon and Xerces-J believe the document is valid against the schema.

With the original schema, on the other hand, Xerces-J gives me the same error message you report; I do not know why. I think this is the second error involved.

相关阅读:
Top