Pregunta

I have searched SO for an answer, found responses, but the solutions don't seem to be working for me. Below is my xml and my schema. The idea is quite simple: no two books can share the same isbn, but when the isbn across two books is equal, eclipse doesn't throw me an error.

The ns prefix is uk.

<library>
    <book>
        <isbn>
            1
        </isbn>
        <title>
        Bernard Williams:  A Biography
        </title>
    </book>
    <book>
        <isbn>
            1
        </isbn>
        <title>
        Principles of Microbiopsychonomics 
        </title>
    </book>
</library>

<xs:element name="root">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="library">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="book" maxOccurs="unbounded">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="isbn" type="xs:integer"/>
                                    <xs:element name="title" />
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            <xs:unique name="uniquebook">
                <xs:selector xpath="uk:library/uk:book"/>
                <xs:field xpath="isbn"/>
            </xs:unique>
            </xs:element>               
        </xs:sequence>
    </xs:complexType>
</xs:element>

¿Fue útil?

Solución

You're close. But you've got two things to clean up here.

First, put your uniqueness constraint on the scope within which you want the uniqueness to be enforced, and write the selector expression to work with that as the context node. (In the code you show, the uniqueness constraint is in the declaration of the library element, and the selector 'uk:library/uk:book' is not going to select anything. Either you want the ISBN to be unique within the document, or you want it unique only within a given library. In the former case, move the declaration to the declaration of uk:root, in the latter drop the first step from the selector path.)

Second, clean up your namespaces story. You don't show the relevant namespace declarations, so I am guessing that

  • In the schema document, the prefix uk is bound to the target namespace of the schema.
  • In the XML instance, that namespace is the default namespace.

If those aren't the case, you need either to make them the case or to follow some other story about your namespaces and follow it consistently.

Right now, the biggest inconsistency is that in your two XPath expressions you assume that the elements uk:library and uk:book will be namespace-qualified, and that the element isbn will not be namespace-qualified. The part of your schema you show suffices to show that those cannot both be the case. Either the schema document specifies elementFormDefault="qualified" on its xs:schema element, in which case you need to add uk: to the field path, or it specifies elementFormDefault="unqualified" (or does not specify the attribute at all), in which case you need to remove the namespace prefixes from the selector path.

If you really want library and book to be namespace-qualified and isbn to be unqualified, you need to use the form attribute on at least one of them.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top