1

I'm trying to use unique element to ensure uniqueness of one of the attributes. Attribute cisloStavu of element stav that I want to be unique has the same value for it's two instances, but validator say's it's OK. I have read many guides about this, and now I'm quite confused, because it looks like I do it the right way.

Here is my XML file (important lines are commented):

<?xml version="1.0" encoding="UTF-8"?>
<Automat nazevKrizovatky="" xmlns="urn:x-fido:schemata:automat-severka" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:x-fido:schemata:automat-severka automat.xsd ">
  <stav cisloStavu="0" interval="0">                    <-- first occurence of cisloStavu="0" -->
    <nasledujiciStav>0</nasledujiciStav>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
  </stav>
  <stav cisloStavu="0" interval="0">               <-- second occurence of cisloStavu="0" -->
    <nasledujiciStav>0</nasledujiciStav>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semafor cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
    <semaforProChodce cisloSemaforu="0" stavSemaforu="zelena"/>
  </stav>
</Automat>

and here is the scheme:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  targetNamespace="urn:x-fido:schemata:automat-severka" 
            elementFormDefault="qualified" 
            xmlns:xs="http://www.w3.org/2001/XMLSchema" 
            xmlns="urn:x-fido:schemata:automat-severka">



    <xs:simpleType name="intervalType">
        <xs:restriction base="xs:nonNegativeInteger"></xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="cisloStavuType">
        <xs:restriction base="xs:nonNegativeInteger"></xs:restriction></xs:simpleType>

    <xs:simpleType name="nasledujiciStavType">
        <xs:restriction base="xs:nonNegativeInteger"></xs:restriction>
    </xs:simpleType>

    <xs:complexType name="StavType">
        <xs:sequence>
            <xs:element name="nasledujiciStav" type="nasledujiciStavType" minOccurs="1" maxOccurs="unbounded">
            </xs:element>
            <xs:element name="semafor" type="semaforType" minOccurs="8" maxOccurs="8">
            </xs:element>
            <xs:element name="semaforProChodce" type="semaforProChodceType" minOccurs="8" maxOccurs="8"></xs:element>
        </xs:sequence>
        <xs:attribute name="cisloStavu" type="cisloStavuType" use="required"></xs:attribute>
        <xs:attribute name="interval" type="intervalType" use="required"></xs:attribute>
    </xs:complexType>

    <xs:complexType name="AutomatType">
        <xs:sequence>
            <xs:element name="stav" type="StavType" maxOccurs="unbounded"></xs:element>
        </xs:sequence>
        <xs:attribute name="nazevKrizovatky" type="nazevKrizovatkyType" use="required"></xs:attribute>
    </xs:complexType>

    <xs:element name="Automat" type="AutomatType">
        <xs:unique name="unikatni_stav">            <-- unique element -->
            <xs:selector xpath="stav" />
            <xs:field xpath="@cisloStavu" />
        </xs:unique>
    </xs:element>

    <xs:simpleType name="nazevKrizovatkyType">
        <xs:restriction base="xs:string"></xs:restriction>
    </xs:simpleType>


    <xs:simpleType name="cisloSemaforuType">
        <xs:restriction base="xs:nonNegativeInteger"></xs:restriction></xs:simpleType>

    <xs:simpleType name="stavSemaforuType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="zelena"></xs:enumeration>
            <xs:enumeration value="červena"></xs:enumeration>
            <xs:enumeration value="pozorZelena"></xs:enumeration>
            <xs:enumeration value="pozorČervena"></xs:enumeration>
            <xs:enumeration value="zelenaVprvavo"></xs:enumeration>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="semaforType">
        <xs:attribute name="cisloSemaforu" type="cisloSemaforuType" use="required"></xs:attribute>
        <xs:attribute name="stavSemaforu" type="stavSemaforuType" use="required"></xs:attribute>
    </xs:complexType>

    <xs:complexType name="semaforProChodceType">
        <xs:attribute name="cisloSemaforu" type="cisloSemaforuType" use="required"></xs:attribute>
        <xs:attribute name="stavSemaforu" type="stavSemaforuProChodceType" use="required"></xs:attribute>
    </xs:complexType>

    <xs:simpleType name="stavSemaforuProChodceType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="zelena"></xs:enumeration>
            <xs:enumeration value="červena"></xs:enumeration>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
2
  • It is a bit hard to understand exactly what is the problem, could you tell us what is wrong compared to how you think it should be? Commented Nov 15, 2012 at 22:26
  • I think the validator should report an error, because the cisloStavu attribute is zero in both stav elements, but it should be unique. But validator says it's ok. Commented Nov 15, 2012 at 22:31

1 Answer 1

1

Finally I've found the problem. I used default namespace (not to be confused with no namespace), and I forgot that xpath expressions (that is in unique element) ignore default namespace. In other words default namespace elements can't be reached by xpath expression.

I added the prefix to namespace in XML and also to xpath expression in unique element, and now it works fine!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.