【问题标题】:How to define a simple element and attribute in XSD如何在 XSD 中定义简单的元素和属性
【发布时间】:2015-07-07 14:30:18
【问题描述】:

我的 XML 如下所示:

<test attri="value">content</test>

我无法在 XML Schema 中构建它,因为我无法提供 complexType 内容,也无法为 simpleType 提供属性。我觉得应该有一个我忽略的非常简单的解决方案。

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    使用以下内容:

     <xs:element name="test ">
            <xs:complexType>
                <xs:simpleContent>
                    <xs:extension base="xs:string">
                        <xs:attribute name="attri" type="xs:string"/>
                    </xs:extension>
                </xs:simpleContent>
            </xs:complexType>
        </xs:element>
    

    【讨论】:

    • 正确 (+1) 并且足够接近,我们称之为平局! ;-)
    【解决方案2】:

    XSD 1.0

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
      <xs:element name="test">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="xs:string">
              <xs:attribute name="attri" type="xs:string"/>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多