XSD 使用断言进行验证

问题描述 投票:0回答:1

只是想知道是否可以使用 XSD 来针对其他元素/属性验证属性。例子如下:

<foods>
<food type="meat">
    <name>Chicken</name>
</food>
<food type="meat">
    <name>Beef</name>
</food>
<food type="meat">
    <name>Pork</name>
</food>
<food type="fruit">
    <name>Banana</name>
</food>
<food type="fruit">
    <name>Apple</name>
</food>
<food type="vegetable">
    <name>Carrot</name>
</food>
<recon vegetable="1" fruit="2" meat="3"/>

是否可以定义一个 XSD 来验证侦察属性的类型以及其中的值?例如,以下 XPath 标识存在“type”=“meat”的三个“food”元素

count(/foods/food[@type='meat'])

我正在研究断言的使用,如下

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="food" type="foodType"/>
<xs:complexType name="foodType">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="type">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="meat"/>
                <xs:enumeration value="vegetable"/>
                <xs:enumeration value="fruit"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>
<xs:element name="foods">
    <xs:annotation>
        <xs:documentation>Comment describing your root element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="food" maxOccurs="unbounded"/>
            <xs:element ref="recon"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:element name="recon" type="reconType"/>
<xs:complexType name="reconType">
    <xs:attribute name="fruits">
        <xs:simpleType>
            <xs:restriction base="xs:short">
                <xs:assertion test="count(/foods/food[@type='fruit']=.)"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="vegetables">
        <xs:simpleType>
            <xs:restriction base="xs:short">
                <xs:assertion test="count(/foods/food[@vegetable])=."/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="meats">
        <xs:simpleType>
            <xs:restriction base="xs:short">
                <xs:assertion test="count(/food/foods[@type='meat'])=."/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>

但是,我在 XML Spy 中遇到错误。任何想法将不胜感激

xml xsd
1个回答
0
投票

您可以使用以下内容:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="food" type="foodType"/>
<xs:complexType name="foodType">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="type">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="meat"/>
                <xs:enumeration value="vegetable"/>
                <xs:enumeration value="fruit"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:complexType>
<xs:element name="foods">
    <xs:annotation>
        <xs:documentation>Comment describing your root element</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="food" maxOccurs="unbounded"/>
            <xs:element ref="recon"/>
        </xs:sequence>
        <xs:assert test="count(/foods/food[@type='fruit']) eq /foods/recon/@fruits"/>
    </xs:complexType>
</xs:element>
<xs:element name="recon" type="reconType"/>
<xs:complexType name="reconType">
    <xs:attribute name="fruits" type="xs:integer"/>
    <xs:attribute name="vegetables" type="xs:integer"/>
    <xs:attribute name="meats" type="xs:integer"/>
</xs:complexType>
</xs:schema>

在显示的

<xs:assert test="count(/foods/food[@type='fruit']) eq /foods/recon/@fruits"/>
之后对其他两个条件进行建模。

在实例中,属性名称需要与模式匹配,例如

<recon vegetables="1" fruits="2" meats="3"/>

© www.soinside.com 2019 - 2024. All rights reserved.