Search code examples
xmlxsd

Can an XML Schema sequence contain two element children named identically?


Here is a fragment of the XSD that I am trying to parse:

<xsd:complexType name="breakdownElementStructureRef">
    <xsd:sequence minOccurs="0">
        <xsd:element name="beUsageRef" type="breakdownElementUsageInBreakdownRef"/>
        <xsd:element name="beUsageRef" type="breakdownElementUsageInBreakdownRef"/>
    </xsd:sequence>
    <xsd:attribute name="uidRef" use="optional">
        <xsd:simpleType>
            <xsd:restriction base="uidRef">
                <xsd:pattern value="bes[1-9][0-9]*"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:attribute name="uriRef" type="xsd:anyURI" use="optional"/>
    <xsd:assert test="(@uidRef and not(beUsageRef) and not(@uriRef)) or (not(@uidRef) and beUsageRef and not(@uriRef)) or (not(@uidRef) and not(beUsageRef) and @uriRef)"/>
</xsd:complexType>

For those who want more context, this is a fragment from this full schema.

Specifically my question relates to the xsd:element name="beUsageRef"... that is duplicated.

I am expecting this to be illegal XSD, but I could be wrong which is why I am looking for a definitive - yes it's legal, or no that is a mistake.

I have a possibly erroneous idea that each child of a complexType must be named, which is why I am expecting this to be in fact be illegal and a mistake in the XSD.

XML gurus out there, am I showing my relational DB bias and this is perfectly fine--a way to ensure that that the child repeats in multiples of 2 if at all?

I am specifically not asking about the semantic intent of the author of the XSD. I understand the xsd:assert specifies only one of beUsageRef, or uidRef or uriRef can exist, which implies a pattern of 2 "beUsageRef" children does not make sense since neither of the attributes can be duplicated, but I am not looking for that level of analysis.

I have been to the w3c and read the XSD spec. I couldn't parse the mathematical representations there to successfully answer my question -- I couldn't figure out if I should be trying to follow rules on a complexType, or on particles. So I seek the wisdom of my betters.

For this question, I just want to know if the fragment I posted is legal, valid, correct XSD? I just want to know if that fragment breaks an XSD rule of any kind.


Solution

  • I am looking for a definitive - yes it's legal, or no that is a mistake.

    Yes, it's legal.

    this is perfectly fine--a way to ensure that that the child repeats in multiples of 2 if at all?

    Almost right. Not multiples, because the sequence containing these two element refs is not allowed to repeat. The only two allowable options are

    • no occurrences of beUsageRef, because the enclosing sequence has minOccurs="0"
    • 2 occurrences of beUsageRef, because if the first occurs in the XML then the enclosing sequence has occurred. Therefore the second beUsageRef must also occur (both element refs have minOccurs=1, implicitly).

    I couldn't figure out if I should be trying to follow rules on a complexType, or on particles.

    The element refs pointing to beUsageRef are particles because they are members of a sequence group.

    I just want to know if that fragment breaks an XSD rule of any kind.

    This may help with similar questions: https://www.utilities-online.info/xsdvalidation