在 XML 中直接使用数据类型

Use datatype directly with in XML

我正在尝试使用以下 XSD:

验证以下 XML

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>

我面临 2 个问题:

无法在 XML 中直接使用 xsd:unsignedInt,出现以下错误

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

无法同时使用 xsd:type 和其他非命名空间属性

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 节点都应包含 IdFlag 属性,并且应根据提供的类型(在实际的 XML 本身中)验证其值。

在 xsd 中使用 xsi:type 对我来说不是一个选择。

由于 xsi:type 必须从 XSD 中指定的类型派生,并且由于 Parameter 在 XSD 中定义为复数,所以您将可能想要创建参数的子项,比如 Valuexsd:anySimpleType,然后您可以通过 XML 文档中的 xsi:type 成功覆盖。

XML

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

How do I edit my XSD to fix these issues.

假设它意味着不触及 XML,那么考虑到分配给 xsi:type 属性的含义,就没有办法让它与纯 XSD 一起工作。 xsd:unsignedInt是简单类型,你的元素是复杂类型,内容简单。

如果您只能使用一种内置 XSD 类型,那么唯一的方法就是更改您的 XML; Kenneth 的回答中描述了一种方法。

如果您可以使用自己的类型,那么类似这样的东西就可以了:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xtm="http://paschidev.com/schemas/metadata/xtm">
    <xsd:element name="Device">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Parameter" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>     
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="FlaggedParameter">
        <xsd:simpleContent>
            <xsd:extension base="xsd:unsignedInt">
                <xsd:attribute name="Id" type="xsd:int" use="required"/>
                <xsd:attribute name="Flag" type="xsd:int" use="required"/>

            </xsd:extension>
        </xsd:simpleContent>
    </xsd:complexType>
</xsd:schema>

此示例将按预期工作:

<Device xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Parameter Id="100" Flag="100" xsi:type="FlaggedParameter">100</Parameter>
</Device>