Two xsd:id and xsd:idref - XSL:SCHEMA

3.4k views Asked by At

i need to do a schema with one tag (A) with one attribute (A-Attr), another tag (B) with another attribute (B-Attr) and a third tag (C) with two attributes (A-Attr and B-Attr).

I don't know what i'm doing wrong. I think it might be related with id and idref but i don't know how to solve it.

<xsd:element name="students">
    <xsd:complexType>
        <xsd:sequence>
            <!-- Elemento que contendrĂ¡ los alumnos, solo puede haber 1 -->
            <xsd:element maxOccurs="1" ref="students"/> 
            <!-- Elemento que contendrĂ¡ las asignaturas, solo puede haber 1 -->
            <xsd:element maxOccurs="1" ref="cursos"/>
            <!-- Elemento que contendrĂ¡ las notas, solo puede haber 1 -->  
            <xsd:element maxOccurs="1" ref="grades"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
<!-- Todo lo relacionado con alumnos, el elemento alumnos tendrĂ¡ los elementos alumno que hagan falta-->
<xsd:element name="students">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" ref="student"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
<!-- El elemento alumno tiene 4 elementos y un atributo con el codigo, que sera la clave primaria -->
<xsd:element name="student">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="1" ref="namelastname"/>
            <xsd:element maxOccurs="1" ref="adress"/>
            <xsd:element maxOccurs="1" ref="number"/>
            <xsd:element maxOccurs="1" ref="tel"/>
        </xsd:sequence>
        <xsd:simpleContent>
            <xsd:extension base="xs:string">
                <xsd:attribute name="cod_student" use="required" type="xs:ID">
                    <xsd:restriction base="xs:string">
                        <xsd:pattern value="[a-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"/>
                    </xsd:restriction>
                </xsd:attribute>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:element>

<xsd:element name="namelastname" type="xs:string"/>
<xsd:element name="adress" type="xs:string"/>
<xsd:element name="number" type="xs:string"/>
<xsd:element name="tel" type="xs:string"/>

<xsd:element name="cursos">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" ref="curso"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:element name="curso">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="1" ref="name"/>
        </xsd:sequence>
        <xsd:simpleContent>
            <xsd:extension base="xs:string">
                <xsd:attribute name="cod_asig" use="required" type="xs:ID">
                    <xsd:restriction base="xs:string">
                        <xsd:pattern value="[a-z][0-9]"/>
                    </xsd:restriction>
                </xsd:attribute>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:element>

<xsd:element name="name" type="xs:string"/>

<xsd:element name="grades">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" ref="grade"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

<xsd:element name="grade">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element maxOccurs="1" ref="points"/>
        </xsd:sequence>
        <xsd:simpleContent>
            <xsd:extension base="xs:string">
                <xsd:attribute name="stud" use="required" type="xs:IDREF"/>
                <xsd:attribute name="grad" use="required" type="xs:IDREF"/>
            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:element>

<xsd:element name="points" type="xs:string"/>

1

There are 1 answers

1
Ghislain Fourny On

xs:ID and xs:IDREF are types that are inherited from DTD validation and that XML Schema took over with compatible semantics. Their semantics is that IDs must be unique within the entire XML document, across all elements no matter what their names are. It means, in particular, that the third tag C may be incorrectly referencing B when A is expected, and vice-versa, so that this could lead to inconsistencies.

More fine-grained keys and foreign keys can and should be achieved by using the more modern and XML-Schema-specific xs:key and xs:keyRef. There are a few StackOverflow questions that explain these.