模式并使用选项作为文档根

模式并使用选项作为文档根

本文介绍了xml 模式并使用选项作为文档根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新手 xml 架构问题.我相信答案是我需要做的事情在模式下是不可能的,但我想确定.问题是我有一个 web 服务,它在成功时返回一种类型的根元素的响应(比如 ),并且在完全失败时返回一个具有不同根元素的文档(比如 ).所以,基本上,两个完全不同的文件:

I have a bit of a newbie xml schema question. I believe the answer is that what I need to do is not possible with schema, but I'd like to be sure. The problem is that I have a webservice that returns a response with one type of root element on success (say <Response>), and on a complete failure, returns a document with a different root element (say, <Exception>). So, basically, two completely different documents:

<Response>......</Response> 或
<异常>....</异常>

<Response>......</Response> OR
<Exception>....</Exception>

是否可以用一个模式文档来描述这两个不同的文档?就像我想要一个选项作为模式元素下的第一个元素——但这不是有效的语法.我尝试了几个解析为有效 xsd 的变体,但不验证文档.有什么建议?或者这根本不可能?非常感谢提前 - m

Is it possible to describe these two different documents with one schema document? It's like I want a choice as the first element under the schema element -- but that isn't valid syntax. I've tried a couple of variants that parse as valid xsd, but don't validate the documents. Any suggestions? Or is this simply not possible? Thanks very much in advance -- m

推荐答案

实际上,XML 模式确实允许您在单个模式中定义替代根元素,尽管不能使用 choice 元素.相反,您需要做的就是将每个可能的根列为您的 schema 元素的直接子元素.

Actually, XML schema does allow you to define alternative root elements in a single schema, although not by using the choice element. Instead, all you need to do is list each of the possible roots as direct children of your schema element.

例如,给定以下 XML 架构:

For example, given the following XML schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="foo">
        ...
    </xs:element>
    <xs:element name="bar">
        ...
    </xs:element>
</xs:schema>

以下任一文件都会对其进行验证:

Either of the following documents would validate against it:

<foo>
    ...
</foo>

或者:

<bar>
    ...
</bar>

这篇关于xml 模式并使用选项作为文档根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:15