如何指定不需要按顺序排列的必需 xml 子元素列表?

发布于 2024-10-04 13:51:56 字数 1258 浏览 1 评论 0原文

以下 xml 根据提供的 xsd 文档进行验证。但是,当我开始从 xml 中删除元素时,它仍然有效!?

如何编写强制包含元素的 xsd?

<?xml version="1.0" encoding="UTF-8"?>
<Video>
  <Title>
  </Title>
  <Description>
  </Description>
  <Contributor>
  </Contributor>
  <Subject>
  </Subject>
</Video>

然后我有 xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="UploadXSD"
    targetNamespace="http://tempuri.org/UploadXSD.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/UploadXSD.xsd"
    xmlns:mstns="http://tempuri.org/UploadXSD.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="Video">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Title" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Description" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Contributor" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Subject" minOccurs="1" type="xs:string"></xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

The following xml validates against the supplied xsd document. However when i start removing elements like from the xml it STILL validates!?

How do I write an xsd that forces the inclusion of elements?

<?xml version="1.0" encoding="UTF-8"?>
<Video>
  <Title>
  </Title>
  <Description>
  </Description>
  <Contributor>
  </Contributor>
  <Subject>
  </Subject>
</Video>

Then I have the xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="UploadXSD"
    targetNamespace="http://tempuri.org/UploadXSD.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/UploadXSD.xsd"
    xmlns:mstns="http://tempuri.org/UploadXSD.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="Video">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Title" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Description" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Contributor" minOccurs="1" type="xs:string"></xs:element>
        <xs:element name="Subject" minOccurs="1" type="xs:string"></xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2024-10-11 13:51:56

如果您使用 xs:all 而不是 xs:sequence,则元素可以按任何顺序出现。还有一些额外的限制 - 例如,不能使用 all 多次指定元素(我不确定您是否打算这种用法,您的架构当前允许这样做。)

If you use xs:all as opposed to xs:sequence, the elements may appear in any order. There are some additional restrictions - for example, elements can not be specified multiple times using all (I'm not sure if you intend that usage, your schema would currently allow that.)

烟织青萝梦 2024-10-11 13:51:56

xsd 中的 targetNamespace 应与您尝试验证的 xml 的命名空间匹配,即

<Video xmlns="http://tempuri.org/UploadXSD.xsd">
  <Title>
  </Title>
  <Description>
  </Description>
  <Contributor>
  </Contributor>
  <Subject>
  </Subject>
</Video>

Your targetNamespace in the xsd should match the namespace of the xml you are trying to validate, i.e.

<Video xmlns="http://tempuri.org/UploadXSD.xsd">
  <Title>
  </Title>
  <Description>
  </Description>
  <Contributor>
  </Contributor>
  <Subject>
  </Subject>
</Video>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文