【问题标题】:xml schema choice selecting one item option or the other with a common partxml 模式选择选择一个项目选项或另一个具有公共部分的选项
【发布时间】:2022-01-05 20:15:46
【问题描述】:

我有一个关于 xml 架构的问题。我真的不知道如何正确使用选择。 这是我的 xml 文本。

<serials>
     <serial>
        <title></title>
        <country></country>
        <director></director>
        <release date></release date>
     </serial>
     .
     .some of the same as the one above
     .
     <serial>
        <title></title>
        <country></country>
        <director></director>
        <uncertaine></uncertainee>
     </serial>
     <serial>
        <title></title>
        <country></country>
        <director></director>
        <scheduled time></scheduled time>
     </serial>
</serials>

这是 xml 架构的问题部分

<xsd:group name="serialData">
     <xsd:choice>
           <xsd:sequence>
                <xsd:element name="title" type="xsd"string"/>
                <xsd:element name="country" type="xsd"string"/>
                <xsd:element name="director" type="xsd"string"/>
                <xsd:element name="release date" type="xsd"string"/>
           </xsd:sequence>
           <xsd:sequence>
                <xsd:element name="title" type="xsd"string"/>
                <xsd:element name="country" type="xsd"string"/>
                <xsd:element name="director" type="xsd"string"/>
                <xsd:element name="scheduled time" type="xsd"string" minOccurs="0" maxOccurs="1"/>
                <xsd:element name="uncertaineetime" type="xsd"string" minOccurs="0" maxOccurs="1"/>
           </xsd:sequence>
     </xsd:choice>
</xsd:group>

问题是后者非典型系列想要与前者相同的属性,但不能拥有它们。

【问题讨论】:

  • 您的 XML 格式不正确。标签名称中不允许有空格。在修复 XML 之前,您无法解决 XML 架构问题。
  • 这里的起始节点也不匹配结束 并且不匹配您架构中的名称。
  • 另外,您的示例与 XSD 中的结构不匹配,因为您在同一个示例中没有预定时间和不确定时间

标签: xml xsd xsd-validation


【解决方案1】:

假设您的有效负载看起来像这样,它是有效的 XML

<serials>
     <serial>
        <title>Title</title>
        <country>Country</country>
        <director>Director</director>
        <release_date>2021-12-06</release_date>
     </serial>
     <serial>
        <title>Title2</title>
        <country>Country</country>
        <director>Director</director>
        <uncertain_time>Uncertain</uncertain_time>
     </serial>
     <serial>
        <title>Title3</title>
        <country>Country</country>
        <director>Director</director>
        <scheduled_time>Scheduled</scheduled_time>
     </serial>
</serials>

您可以简单地使用如下架构,将其中的一些设置为可选。

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="serials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="serial">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" />
              <xs:element name="country" type="xs:string" />
              <xs:element name="director" type="xs:string" />
              <xs:element minOccurs="0" name="scheduled_time" type="xs:string" />
              <xs:element minOccurs="0" name="uncertain_time" type="xs:string" />
              <xs:element minOccurs="0" name="release_date" type="xs:date" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

如果您的意思是您可以有不确定时间或计划时间或发布日期,那么这些就是您按以下方式放入选择节点中的项目

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="serials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="serial">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="title" type="xs:string" />
              <xs:element name="country" type="xs:string" />
              <xs:element name="director" type="xs:string" />
              <xs:choice>
                <xs:element name="scheduled_time" type="xs:string" />
                <xs:element name="uncertain_time" type="xs:string" />
                <xs:element name="release_date" type="xs:date" />
              </xs:choice>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    相关资源
    最近更新 更多