XSD 多个同名但不同类型的元素 [英] XSD multiple elements with same name but different types

查看:33
本文介绍了XSD 多个同名但不同类型的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试针对一些看起来(结构上明智)如下所示的 XML 编写 XSD:

<响应><id>foo</id><值><value>bar</value></values></响应><响应><id>asdf</id><值><价值><a>a</a><b>b</b><c>c</c></values></响应></响应>

当我尝试验证(使用 javax API)时,我收到以下错误消息:

模型组中出现多个名称为'response'的元素,类型不同

是否可以编写一个 XSD 来验证具有相同名称的多个节点?各个响应节点的顺序是一致的,所以这部分至少不会成为问题.

解决方案

在设计层面,要问的问题是为什么要指定两个具有不同有效性条件的不同元素,然后使用它们的元素类型名称来表示他们是一样的东西.你认为以这种方式向处理器撒谎是好的设计吗?

在模式级别:分配给元素的类型完全由验证根的路径决定.(如果验证从文档根开始,那么在您的示例中,response 元素的类型必须完全由路径 /responses/response 决定.这是完全合法的有两个同名元素出现在不同的上下文中(其中上下文"仅由祖先而不是兄弟定义);在 XSD 中不允许为两个同名的兄弟使用不同的默认类型.

I'm trying to write an XSD against some XML that looks (structurally wise) something like this:

<responses>

    <response>
        <id>foo</id>
        <values>
            <value>bar</value>
        </values>
    </response>

    <response>
        <id>asdf</id>
        <values>
            <value>
                <a>a</a>
                <b>b</b>
                <c>c</c>
            </value>
        </values>
    </response>

</responses>

When I try to validate (using javax APIs) it, I'm getting the following error message:

Multiple elements with name 'response', with different types, appear in the model group

Is it possible to write an XSD that validates multiple nodes with the same names like this? The order in which the individual response nodes is consistent, so that part wouldn't be an issue at least.

解决方案

At the design level, the question to ask is why you wish to specify two different elements with different validity conditions, and then use their element type names to say that they are the same thing. Do you believe that lying to the processor in this way is good design?

At the schema level: the type to be assigned to an element is completely determined by the path from the validation root. (If the validation starts at the document root, then in your example the type of a response element must be completely determined by the path /responses/response. It is perfectly legal to have two elements with the same name which appear in different contexts (where 'context' is defined solely by ancestry and not by siblings); it is not allowed in XSD to have different default types for two siblings of the same name.

这篇关于XSD 多个同名但不同类型的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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