XSD 1.1 Assert to Count and Compare Elements

怎甘沉沦 提交于 2019-12-06 11:27:47

I think it makes most sense to have the assert in the type definition for the abc:Individual element, then the assert is simply:

count(abc:Country) eq count(abc:AccountNumber)

The complete schema is like so. For simplicity I kept AccountNumber in the abc namespace, but it can be easily replaced with a reference otherwise.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:abc="http://www.example.com/abc"
    targetNamespace="http://www.example.com/abc"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
    vc:minVersion="1.1">
    <xs:element name="Account">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="abc:Individual" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Individual">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="abc:Country" maxOccurs="unbounded" />
                <xs:element ref="abc:AccountNumber" maxOccurs="unbounded" />
            </xs:sequence>
            <xs:assert test="count(abc:Country) eq count(abc:AccountNumber)"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="Country" type="xs:string"/>
    <xs:element name="AccountNumber">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:string">
                    <xs:attribute name="issuedBy" type="xs:string"/>
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>
</xs:schema>

Apart from changing abs to abc, the original document validates successfully against the schema, i.e.:

<?xml version="1.0" encoding="UTF-8"?>
<abc:Account
    xmlns:abc="http://www.example.com/abc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.example.com/abc test.xsd">
    <abc:Individual>
        <abc:Country>Germany</abc:Country>
        <abc:Country>Australia</abc:Country>
        <abc:AccountNumber issuedBy="DE">123456</abc:AccountNumber>
        <abc:AccountNumber issuedBy="AU">654321</abc:AccountNumber>
    </abc:Individual>
</abc:Account>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!