断言无效、放错地方或发生得太频繁



我在尝试生成XSD时遇到了这个错误,无法找出我做错了什么;我的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="FICH">
<xs:complexType>
<xs:sequence>
<xs:element name="cabeza">
<xs:complexType>
<xs:sequence>
<xs:element name="VERSION_ORIG" type="Version"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DATA_MAJ">
<xs:complexType>
<xs:sequence>
<xs:element name="DATA1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="VERSION_SIT_NOM" type="Version"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:assert test="CABEZA/VERSION_ORIG = DATA_MAJ/VERSION_SIT_NOM"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="YYMM">
<xs:restriction base="xs:integer">
<xs:pattern value="[0-9]{2}[01|02|03|04|05|06|07|08|09|10|11|12]"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="Version">
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{2}.[0-9]{2}.[0-9]{2}"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="emptyString">
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:schema>

其思想是元素CABEZA/VERSION_ORIG必须等于DATA_MAJ/VERSION_SIT_NOM。我一直在寻找一些解决方案,但对我来说一切都还可以。

提前感谢

您的模式文档对我来说还可以(更重要的是,对Saxon 9来说也是如此)。

因此,错误消息最可能的原因是(1)您正在使用XSD 1.0处理器来处理XSD 1.1模式文档,以及(2)在这种情况下,您的处理器没有提供世界上最好的错误消息。目前(2013年初),Saxon和Xerces J的测试版都支持XSD 1.1。如果您正在使用另一个XSD验证器,您可能需要联系供应商或开发人员,询问他们何时支持XSD1.1。(如果认为用户不在乎,没有人会转向1.1。)

您做错了几件事。让我们从一个经过更正的XSD和匹配的示例XML开始。

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" xmlns:tns="http://www.w3schools.com" elementFormDefault="qualified">
    <xs:element name="FICH">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="cabeza">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="VERSION_ORIG" type="Version"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="DATA_MAJ">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="DATA1" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="VERSION_SIT_NOM" type="Version"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>          
            <xs:assert test="tns:cabeza/tns:VERSION_ORIG = tns:DATA_MAJ/tns:DATA1/tns:VERSION_SIT_NOM"/>
        </xs:complexType>
    </xs:element>
    <xs:simpleType name="YYMM">
        <xs:restriction base="xs:integer">
            <xs:pattern value="[0-9]{2}[01|02|03|04|05|06|07|08|09|10|11|12]"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="Version">
        <xs:restriction base="xs:string">
            <xs:pattern value="[0-9]{2}.[0-9]{2}.[0-9]{2}"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="emptyString">
        <xs:restriction base="xs:string">
            <xs:enumeration value=""/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

无效XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<FICH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3schools.com">
    <cabeza>
        <VERSION_ORIG>22.22.22</VERSION_ORIG>
    </cabeza>
    <DATA_MAJ>
        <DATA1>
            <VERSION_SIT_NOM>22.22.23</VERSION_SIT_NOM>
        </DATA1>
    </DATA_MAJ>
</FICH>

错误消息:

Error while loading [], line 12 position 8
cvc-assertion.3.13.4.1: Assertion evaluation ('tns:cabeza/tns:VERSION_ORIG = tns:DATA_MAJ/tns:DATA1/tns:VERSION_SIT_NOM') for element 'FICH' with type '#anonymous' did not succeed. 
Document1.xml is XSD 1.1 invalid.

解释:

  • 很可能您正在使用XSD 1.0处理器来验证XSD 1.1。注释掉断言,看看错误是否消失
  • 除非您的XSD没有目标命名空间,否则您必须提供一个别名,例如上面的示例XSD中的xmlns:tns,以匹配您的目标命名空间;请确保XPath正确使用前缀。这是有原因的,Xerces(正如@CMSperbergMcQueen所提到的)似乎也非常想要它们
  • XPath完全错了。名称区分大小写,所以cabeza不是cabeza,而且XSD中有一个与XPath不匹配的额外级别;更具体地说,您缺少一个DATA1

最新更新