如何将 xsi:type 定义为 XML 模式中的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21216778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:58:53  来源:igfitidea点击:

How to define xsi:type as an attribute in XML-schema?

xmlxsd

提问by Peter

I have an XML for which I want to write a schema definition. The problem is that I don't know how to define xsi:type as an attribute. Here is the XML element:

我有一个 XML,我想为其编写模式定义。问题是我不知道如何将 xsi:type 定义为属性。这是 XML 元素:

<SerializedData xsi:type="xs:double">300.0</SerializedData>

My XML-schema definition so far looks like this:

到目前为止,我的 XML 架构定义如下所示:

<complexType name="SerializedDataType">
    <simpleContent>
        <extension base="double">

        </extension>
    </simpleContent>
</complexType>

I have also tried defining it like Ian Roberts suggested:

我也尝试过像 Ian Roberts 建议的那样定义它:

<element name="SerializedData"/>

However, when I use BPEL designer to initialize it like this:

但是,当我使用 BPEL 设计器像这样初始化它时:

<SerializedData xsi:type="xs:double">300.0</SerializedData>

I get the following warning:

我收到以下警告:

The fixed value you entered does not appear to be valid XML (which is required for some types of fixed values to work correctly). It will be saved in a text format.

您输入的固定值似乎不是有效的 XML(某些类型的固定值需要它才能正常工作)。它将以文本格式保存。

If I initialize it like this there is no warning:

如果我像这样初始化它,则没有警告:

<SerializedData>300.0</SerializedData>

But the problem is that the Web Service I am trying to invoke expects the request SOAP message to include the attribute xsi:type="xs:double". How can I make my SOAP request message to include it?

但问题是我试图调用的 Web 服务期望请求 SOAP 消息包含属性 xsi:type="xs:double"。如何让我的 SOAP 请求消息包含它?

Any help would be greatly appreciated!

任何帮助将不胜感激!

回答by Ian Roberts

You don't need to - just declare the element without a type at all.

您不需要 - 只需声明完全没有类型的元素。

<element name="SerializedData" />

The xsi:typeattribute is used to indicate to the schema validator that the real type of a particular instance of an element is not the element's declared type but rather a sub-type derived from the declared type. By declaring the element with no type you are saying it can have anytype, and you will use xsi:typein the instance to specify which.

xsi:type属性用于向模式验证器指示元素的特定实例的真实类型不是元素的声明类型,而是从声明类型派生的子类型。通过声明没有类型的元素,您是说它可以具有任何类型,并且您将xsi:type在实例中使用来指定哪种类型。

Strictly you're declaring an element whose type is the "ur-type" which is the root of the XML Schema type hierarchy - all types, simple and complex, ultimately derive from the ur-type. If you want to restrict the SerializedDataelement to simple content only (no sub-elements or attributes) then declare it as

严格来说,您要声明一个元素,其类型是“ur-type”,它是 XML Schema 类型层次结构的根——所有类型,简单和复杂,最终都从 ur-type 派生。如果您想将SerializedData元素限制为仅简单内容(无子元素或属性),则将其声明为

<element name="SerializedData" type="anySimpleType" />

Regarding the second part of your question, your designer tool is right that in isolation

关于您问题的第二部分,您的设计器工具是正确的,孤立地

<SerializedData xsi:type="xs:double">300.0</SerializedData>

is not correct XML, because the xsinamespace has not been declared. Try adding the namespace declarations:

不是正确的 XML,因为xsi尚未声明命名空间。尝试添加命名空间声明:

<SerializedData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xsi:type="xs:double">300.0</SerializedData>

回答by Michael Kay

The xsi:type attribute doesn't need to be declared in the schema: it's implicitly declared, and can be used on any element. But in an instance, it has to be a valid QName.

xsi:type 属性不需要在模式中声明:它是隐式声明的,可以在任何元素上使用。但在一个实例中,它必须是一个有效的 QName。

If you write

如果你写

<SerializedData xsi:type="xs:double">300.0</SerializedData>

then

然后

(a) to be namespace-well-formed, you need to declare the "xsi" namespace

(a) 要成为命名空间格式良好的,您需要声明“xsi”命名空间

(b) to be schema-valid, you also need to declare the "xs" namespace.

(b) 要使模式有效,您还需要声明“xs”命名空间。

As Roberts indicated, this means you should write

正如罗伯茨指出的,这意味着你应该写

<SerializedData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xsi:type="xs:double">300.0</SerializedData>