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

关于XSD验证器在SAX解析中屏蔽多错误的技术咨询

XSD 1.0多Facet违例仅返回单条错误的问题

我需要生成一份按元素统计XSD 1.0 Schema所有违例情况的报告,但测试Xerces、Saxon-EE和Altova三款验证器时发现:即便单个元素同时违反多个facet约束,验证器也只会输出一条错误信息。

比如下面的示例中,文本Brand New Œyes同时违反了maxLength和pattern两个facet,但三款工具都只返回了其中一个错误。


示例XSD Schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root" type="rootType"/>
    <xs:complexType name="rootType">
        <xs:sequence>
            <xs:element name="records" type="recordsType"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="recordsType">
        <xs:sequence>
            <xs:element name="record" type="recordType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="recordType">
        <xs:sequence>
            <xs:element name="title">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:minLength value="1"/>
                        <xs:maxLength value="10"/>
                        <xs:pattern value="[A-Za-z0-9!#$%&amp;'*+/=?^_`{|}~.\- ]+"/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:element>
            <xs:element name="artist" type="xs:string"/>
            <xs:element name="genre" type="xs:string"/>
            <xs:element name="year" type="xs:short"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

待验证XML文档

<root>
    <records>
        <record>
            <title>Brand New Œyes</title>
            <artist>Paramore</artist>
            <genre>Punk Rock</genre>
            <year>2011</year>
        </record>
    </records>
</root>

验证器输出结果

  • Xerces输出:

    cvc-pattern-valid: Value 'Brand New Œyes' is not facet-valid with respect to pattern '[A-Za-z0-9!#$%&'*+/=?_`{|}~.- ]+' for type '#AnonType_titlerecordType'. AND cvc-type.3.1.3: The value 'Brand New Œyes' of element 'title' is not valid.
    

    仅提示pattern相关违例,未提及maxLength错误。

  • Saxon-EE输出:

    The content "Brand New Œyes" of element <title> does not match the required simple type. Value "Brand New Œyes" contravenes the maxLength facet "10" of the type of element title
    

    仅提示maxLength相关违例,未提及pattern错误。


注释pattern约束后的测试结果

若将XSD中的pattern facet注释掉,Xerces才会显示之前被屏蔽的maxLength错误:

cvc-maxLength-valid: Value 'Brand New Œyes' with length = '14' is not facet-valid with respect to maxLength '10' for type '#AnonType_titlerecordType'.

而Saxon-EE原本就只提示maxLength错误,因此该操作对其输出无影响。

内容的提问来源于stack exchange,提问作者J. Nicholas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 21:10:28