如何在 XSD 中使用 xsi:type 限制 XML 元素的值? [英] How to restrict the value of an XML element using xsi:type in XSD?

查看:32
本文介绍了如何在 XSD 中使用 xsi:type 限制 XML 元素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据属性值验证元素的文本值.例如

<设备 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><参数 xsi:type="xsd:unsignedInt">-100</Parameter><参数 xsi:type="xsd:boolean"></Parameter><Parameter>hello</Parameter></设备>

以上两个都应该失败.对于布尔值,除了true"或false"(甚至是空字符串)外,不应接受任何内容

我的 xml 复杂得多,有很多 ObjectParameter 节点,这是我的 xsd,它递归地验证所有这些节点

 <xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element ref="对象"/><xs:element ref="参数"/></xs:choice></xs:complexType><xs:complexType name="objType"><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element ref="对象"/><xs:element ref="参数"/></xs:choice><!-- 在此处添加对象"类型的所有有效属性--><xs:attribute name="Id" use="required"/><xs:attribute name="Flag" use="required"/><xs:anyAttribute processContents="lax"/></xs:complexType><xs:complexType name="paramType" mixed="true"><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element ref="对象"/><xs:element ref="参数"/></xs:choice><xs:attribute name="Id" use="required"/><xs:attribute name="Flag" use="required"/><xs:anyAttribute processContents="lax"/></xs:complexType>

但我正面临这个错误.

类型 'xsd:unsignedInt' 不是从元素 'Parameter' 的类型定义 'paramType' 有效派生的.类型xsd:unsignedInt"不是从元素Parameter"的类型定义paramType"有效派生的.

我该如何解决这个问题?

解决方案

要在 XML 文档中使用 xsi:type:

  1. 声明 xsi 命名空间前缀,通常在根元素上:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. 声明xs(或xsd)命名空间前缀,通常在根元素上:

    xmlns:xs="http://www.w3.org/2001/XMLSchema"

  3. 向您希望约束的元素添加任何 XSD 类型:

    xsi:type="xs:boolean"

这些更改直接在您的 XML 文档中进行.您不必对 XSD 进行任何更改.但是,所选类型必须从 XSD 为给定元素提供的类型有效派生.

<小时>

相关的 W3C 参考

来自

I would like to validate text values of an element based on attribute value. For example

<Device xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Parameter xsi:type="xsd:unsignedInt">-100</Parameter>
  <Parameter xsi:type="xsd:boolean"></Parameter>
  <Parameter>hello</Parameter>
</Device>

Both the above should fail. For boolean, anything shouldn't be accepted except "true" or "false" (even empty string)

My xml is much more complex with lot of Object and Parameter nodes and this is my xsd which is validating all those nodes recursively

    <xs:complexType name="deviceType">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="Object"/>
                    <xs:element ref="Parameter"/>
            </xs:choice>
    </xs:complexType>

    <xs:complexType name="objType">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="Object"/>
                    <xs:element ref="Parameter"/>
            </xs:choice>
            <!-- Add all valid attributes for 'Object' type here -->
            <xs:attribute name="Id" use="required"/>
            <xs:attribute name="Flag" use="required"/>
            <xs:anyAttribute processContents="lax"/>
    </xs:complexType>

    <xs:complexType name="paramType" mixed="true">
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element ref="Object"/>
                    <xs:element ref="Parameter"/>
            </xs:choice>
            <xs:attribute name="Id" use="required"/>
            <xs:attribute name="Flag" use="required"/>
            <xs:anyAttribute processContents="lax"/>
    </xs:complexType>

But I'm facing this error.

Type 'xsd:unsignedInt' is not validly derived from the type definition, 'paramType', of element 'Parameter'.
Type 'xsd:unsignedInt' is not validly derived from the type definition, 'paramType', of element 'Parameter'.

How can I fix this?

解决方案

To use xsi:type in your XML document:

  1. Declare the xsi namespace prefix, usually on the root element:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    

  2. Declare the xs (or xsd) namespace prefix, usually on the root element:

    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    

  3. Add any XSD type to the element you wish to constrain:

    xsi:type="xs:boolean"
    

These changes are made directly in your XML document. You do not have to make any changes to your XSD. However, the chosen type must be validly derived from the type provided by the XSD for the given element.


Relevant W3C References

From XML Schema Part 1: Structures Second Edition...

2.6.1 xsi:type

The Simple Type Definition (§2.2.1.2) or Complex Type Definition (§2.2.1.3) used in ·validation· of an element is usually determined by reference to the appropriate schema components. An element information item in an instance may, however, explicitly assert its type using the attribute xsi:type. The value of this attribute is a ·QName·; see QName Interpretation (§3.15.3) for the means by which the ·QName· is associated with a type definition.

[...]

Validation Rule: Element Locally Valid (Element)

4.3 The ·local type definition· must be validly derived from the {type definition} given the union of the {disallowed substitutions} and the {type definition}'s {prohibited substitutions}, as defined in Type Derivation OK (Complex) (§3.4.6) (if it is a complex type definition), or given {disallowed substitutions} as defined in Type Derivation OK (Simple) (§3.14.6) (if it is a simple type definition).

From XML Schema Part 2: Datatypes Second Edition...

3 Built-in datatypes

这篇关于如何在 XSD 中使用 xsi:type 限制 XML 元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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