我想基于属性值验证元素的文本值.例如
<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>
Run Code Online (Sandbox Code Playgroud)
以上都应该失败.对于布尔值,除"true"或"false"(甚至空字符串)外,不应接受任何内容
我的XML是复杂得多用很多Object和Parameter节点,这是我的xsd这是递归地验证所有的节点
<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>
Run Code Online (Sandbox Code Playgroud)
但是我面临这个错误.
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'.
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
xsi:type在XML文档中使用:声明xsi名称空间前缀,通常在根元素上:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Run Code Online (Sandbox Code Playgroud)通常在根元素上声明xs(或xsd)名称空间前缀:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
Run Code Online (Sandbox Code Playgroud)将任何XSD类型添加到您要约束的元素:
xsi:type="xs:boolean"
Run Code Online (Sandbox Code Playgroud)这些更改直接在XML文档中进行.您不必对XSD进行任何更改.但是,所选类型必须从XSD为给定元素提供的类型有效地派生.
从XML Schema第1部分:结构第二版 ......
元素的验证中使用的简单类型定义(第2.2.1.2节)或复杂类型定义(第2.2.1.3节)通常通过引用适当的模式组件来确定.但是,实例中的元素信息项可以使用该属性显式地声明其类型.该属性的值为·QName· ; 有关·QName·与类型定义关联的方法,请参阅QName解释(第3.13.3节).
xsi:type
[...]
4.3 ·本地类型定义·必须从{类型定义}有效地派生,给定{不允许的替换}和{类型定义}的{禁止替换}的并集,如类型派生确定(复杂)中所定义的(§) 3.4.6)(如果它是一个复杂的类型定义),或者给出类型派生OK(简单)(§3.14.6)中定义的{disallowed substitution}(如果它是一个简单的类型定义).
| 归档时间: |
|
| 查看次数: |
4326 次 |
| 最近记录: |