带有 keyref 的 XSD 架构验证错误 [英] XSD Schema validation error with keyref

查看:30
本文介绍了带有 keyref 的 XSD 架构验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些验证器,以下 xsd 在针对它验证似乎有效的 xml 文件时会导致一些问题.根据验证器,错误消息看起来像这样(libxml):

With some validators the following xsd causes some problems when validating xml files against it which seem to be valid. Dependent on the validator the error message looks something like this (libxml):

Schemas validity error : Element 'referringElement': No match found for key-sequence ['1'] of keyref 'reference'. Start location: 8:0 

错误信息令人困惑,因为它似乎定义了引用的键 (id=1).

The error message is confusing because it seems that the referenced key (id=1) was defined.

这是导致问题的 xsd:

This is the xsd which causes problems:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xs:element name="document">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="listOfReferencedElements"/>
                <xs:element ref="referringElement"/>
            </xs:sequence>
        </xs:complexType>
        <xs:keyref name="reference" refer="id">
            <xs:selector xpath=".//*"/>
            <xs:field xpath="@reference"/>
        </xs:keyref>
    </xs:element>

    <xs:element name="listOfReferencedElements">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="referencedElement" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="id" type="xs:string" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:key name="id">
            <xs:selector xpath="./referencedElement"/>
            <xs:field xpath="@id"/>
        </xs:key>
    </xs:element>

    <xs:element name="referringElement">
        <xs:complexType>
            <xs:attribute name="reference" use="required"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

...这是一个示例 xml,导致上面的错误消息:

... and this a sample xml which leads to the error message above:

<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="file:/C:/Users/jakob/dev/projects/integration/trading-contracts/trunk/playground/Reference.xsd">
    <listOfReferencedElements>
        <referencedElement id="1"/>
        <referencedElement id="2"/>
    </listOfReferencedElements>
    <referringElement reference="1"/>
</document>

推荐答案

问题好像是key的类型和引用的类型不兼容.修改引用元素如下(定义类型为xs:string)解决问题:

The problem seems to be that the type of the key is not compatible with the type of the reference. Amending the referring element in the following way (defining the type as xs:string) solves the problem:

<xs:element name="referringElement">
    <xs:complexType>
        <xs:attribute name="reference" type="xs:string"/>
    </xs:complexType>
</xs:element>

这篇关于带有 keyref 的 XSD 架构验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆