如何将 xsi:type 定义为 XML 模式中的属性? [英] How to define xsi:type as an attribute in XML-schema?

查看:38
本文介绍了如何将 xsi:type 定义为 XML 模式中的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

My XML-schema definition so far looks like this:

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

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

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

I have also tried defining it like Ian Roberts suggested:

<element name="SerializedData"/>

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

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

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

我收到以下警告:

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

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.

如果我这样初始化它就没有警告:

If I initialize it like this there is no warning:

<SerializedData>300.0</SerializedData>

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

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?

任何帮助将不胜感激!

推荐答案

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

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

<element name="SerializedData" />

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

The xsi:type attribute 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 any type, and you will use xsi:type in the instance to specify which.

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

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 SerializedData element to simple content only (no sub-elements or attributes) then declare it as

<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>

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

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

<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>

这篇关于如何将 xsi:type 定义为 XML 模式中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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