首页 > 解决方案 > 使用 XSD 1.1 进行 xml 验证

问题描述

我没有使用 XSD 文件验证的经验,我必须使用 XSD 1.1 验证 xml 文件,但出现下一个错误:

lineNumber: 10; columnNumber: 71; c-cta-xpath: The XPath expression '@tipoelemento = CABECERA' couldn't compile successfully in 'cta-subset' mode, during CTA evaluation.

文件.xml

<datos>
<elemento tipoelemento="CABECERA">
    <atributo>
        <nombre>VERSION</nombre>
        <valor>1.0</valor>
    </atributo>
    <atributo>
        <nombre>BRIGADA</nombre>
        <valor>JADSJL</valor>
    </atributo>
    <atributo>
        <nombre>BUZON</nombre>
        <valor>ASDKLFJKA</valor>
    </atributo>
</elemento>

文件.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
       elementFormDefault="qualified"
       vc:minVersion="1.1">
<xs:element name="datos">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="elemento" minOccurs="1" maxOccurs="1">
                <xs:alternative test="@tipoelemento = 'CABECERA'" type="cabecera"/>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
<xs:complexType name="cabecera">
    <xs:sequence>
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="VERSION" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="BRIGADA" />
        <xs:element name="atributo" minOccurs="1" maxOccurs="1" type="BUZON" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="VERSION">
<xs:sequence>
    <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="VERSION" />
    <xs:element name="valor" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BRIGADA">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="BRIGADA" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
<xs:complexType name="BUZON">
    <xs:sequence>
        <xs:element name="nombre" type="xs:string" minOccurs="1" fixed="BUZON" />
        <xs:element name="valor" type="xs:string" minOccurs="1" />
    </xs:sequence>
</xs:complexType>
</xs:schema>

我没有这方面的经验,对不起!

我跟着这个链接点击这里

我在此链接中看到单击此处,我​​需要使用完整的 XPath 2.0,但我不知道该怎么做。

我该如何解决这个问题?

问候

标签: xmlxsdxsd-validationxml-validation

解决方案


条件类型赋值中使用的 XPath 表达式仅限于访问属性,它们不能访问子元素。

您的错误是在您的表达式中,@tipoelemento = CABECERA,CABACERA被解释为child::CABECERA,即对子元素的引用。我想你在这里想要一个字符串文字,这将是@tipoelemento = 'CABECERA'字符串在引号中的位置。


推荐阅读