微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为给定的 XML 片段编写 XSD?

如何解决如何为给定的 XML 片段编写 XSD?

我在编写 xsd 方面还很陌生。我正在尝试为给定的 xml 片段编写代码。但是我对位置元素感到困惑。有两个位置元素。如果给定的 xml 片段是否正确,有人可以帮助我理解我。如果正确,我们如何为这种情况定义 xsd。

<data>
   <location>
      <name>London</name>
      <type />
      <location altitude="0" latitude="51.5085" longitude="-0.1258" />
   </location>
   <credit />
   ...

解决方法

这两个位置对于创建 XSD 来说不是问题。如果以 </data>

终止,以下 XSD 将验证您的有效负载

然而,正如其他人所说,在不知道 type 和 credit 元素的类型的情况下,如果任何元素是可选的或重复多次,它可能不符合您的要求。

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="data">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="location">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string" />
              <xs:element name="type" type="xs:string" />
              <xs:element name="location">
                <xs:complexType>
                  <xs:attribute name="altitude" type="xs:integer" use="required" />
                  <xs:attribute name="latitude" type="xs:decimal" use="required" />
                  <xs:attribute name="longitude" type="xs:decimal" use="required" />
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="credit" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。