You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于使用XSD 1.1断言验证recon属性值与对应food元素数量匹配的问题咨询

关于使用XSD 1.1断言验证recon属性值与对应food元素数量匹配的问题咨询

看起来你找对了方向——XSD 1.1的断言特性完全能实现这种跨节点的关联验证需求!不过你的当前XSD里有几处语法和逻辑错误,导致XML Spy报错,我来帮你修正并解释:

原XSD里的核心问题

  1. XPath路径与语法错误
    • 你写的count(/foods/food[@type='fruit']=.) 把比较运算符放错了位置,正确的写法应该是count(/foods/food[@type='fruit']) = .,要把count()的结果和当前属性值(.代表当前属性)做比较
    • 比如count(/foods/food[@vegetable]) 这里的条件写错了,应该是@type='vegetable',你漏了type属性的引用
    • 还有count(/food/foods[@type='meat']) 路径完全反了,正确的根路径是/foods/food,不是/food/foods
  2. 属性名不匹配
    你的示例XML里recon的属性是vegetable、fruit、meat,但原XSD里写的是复数形式vegetables、fruits、meats,两者要严格对应,否则验证时会识别不到属性。

修正后的完整XSD代码

<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="fruit">
            <xs:simpleType>
                <xs:restriction base="xs:short">
                    <xs:assertion test="count(/foods/food[@type='fruit']) = ."/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="vegetable">
            <xs:simpleType>
                <xs:restriction base="xs:short">
                    <xs:assertion test="count(/foods/food[@type='vegetable']) = ."/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="meat">
            <xs:simpleType>
                <xs:restriction base="xs:short">
                    <xs:assertion test="count(/foods/food[@type='meat']) = ."/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
    </xs:complexType>

</xs:schema>

修正后的关键说明

  • 确保XSD声明了1.1版本(vc:minVersion="1.1"),因为断言是XSD 1.1才支持的特性,旧的1.0版本完全不兼容
  • 每个属性的断言都会计算对应type的food元素数量,再和属性值做相等校验——比如如果你的XML里meat属性写了2,但实际有3个type="meat"的food,验证就会直接报错
  • 路径使用绝对路径/foods/food[@type='xxx'],确保能正确遍历到所有对应类型的食物元素

这样修改后,你的XML Spy应该就能正常执行验证了,完全满足你既要验证recon属性类型,又要验证属性值和对应食物数量匹配的需求!

备注:内容来源于stack exchange,提问作者Andrew Tyson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 09:44:36