在 XML 中直接使用数据类型 [英] Use datatype directly with in XML
问题描述
我正在尝试使用以下 XSD 验证以下 XML:
I'm trying to validate below XML with the below XSD:
XML
<Device xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Parameter Id="100" Flag="100" xsi:type="xsd:unsignedInt">-100</Parameter>
</Device>
XSD
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Device">
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Parameter' type='paramType' minOccurs='0' maxOccurs='unbounded' />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="paramType">
<xsd:attribute name="Id" use="required"/>
<xsd:attribute name="Flag" use="required"/>
<xsd:anyAttribute processContents="lax"/>
</xsd:complexType>
</xsd:schema>
我面临两个问题:
无法直接在 XML 中使用 xsd:unsignedInt
并出现以下错误
Unable to use xsd:unsignedInt
directly in XML with below error
Type 'xsd:unsignedInt' is not validly derived from the type definition, 'paramType', of element 'Parameter'
无法同时使用 xsd:type
和其他非命名空间属性
Unable to use both xsd:type
and other non-namespaced attributes at the same time
Element 'Parameter' is a simple type, so it cannot have attributes, excepting those whose namespace name is identical to 'http://www.w3.org/2001/XMLSchema-instance' and whose [local name] is one of 'type', 'nil', 'schemaLocation' or 'noNamespaceSchemaLocation'. However, the attribute, 'Flag' was found.
如何编辑我的 XSD 以解决这些问题.我的最后一个要求是所有 Parameter
节点都应该包含 Id
和 Flag
属性,并且它的值应该根据提供的类型(在实际的 XML本身).
How do I edit my XSD to fix these issues. My final requirement is that all the Parameter
nodes should contain Id
and Flag
attributes and its value should be validated against type provided (in the actual XML itself).
在 xsd 中使用 xsi:type
不是我的选择.
Using xsi:type
inside xsd is not an option for me.
推荐答案
由于 xsi:type
必须从 XSD 中指定的类型派生,并且由于 Parameter
在 XSD 中定义为复杂的,您可能想要创建 xsd:anySimpleType
的参数的子项,例如 Value
,然后您可以通过 xsd:anySimpleType
成功覆盖它XML 文档中的 code>xsi:type.
Since xsi:type
has to be derivable from the type specified in the XSD, and since Parameter
is defined in the XSD to be complex, you'll probably want to create a child of Parameter, say Value
, of xsd:anySimpleType
that you can then override successfully via xsi:type
in the XML document.
<?xml version="1.0" encoding="UTF-8"?>
<Device xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Parameter Id="100" Flag="100">
<Value xsi:type="xsd:unsignedInt">100</Value>
</Parameter>
</Device>
XSD
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Device">
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Parameter' type='paramType'
minOccurs='0' maxOccurs='unbounded' />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="paramType">
<xsd:sequence>
<xsd:element name="Value" type="xsd:anySimpleType"/>
</xsd:sequence>
<xsd:attribute name="Id" use="required"/>
<xsd:attribute name="Flag" use="required"/>
<xsd:anyAttribute processContents="lax"/>
</xsd:complexType>
</xsd:schema>
这篇关于在 XML 中直接使用数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!