XSD:允许来自不同命名空间的元素在序列中的任何位置 [英] XSD: Allow elements from different namespace anywhere in a sequence

查看:24
本文介绍了XSD:允许来自不同命名空间的元素在序列中的任何位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试以下方法.我想为 XML 创建一个 XSD,其中某些元素只允许出现一次并且需要有效,来自其他命名空间的元素可以在任何地方出现,并且没有他们必须验证的模式.

I've been trying out the following. I want to create an XSD for an XML where certain elements are allowed to appear only once and need to be valid, and elements from other namespaces are allowed anywhere, and have no schema they have to validate with.

应该允许的 XML:

<ns:bookstore>
  <ns:books>
    <ns:book1 />
    <other:magazine1 />
    <ns:book2 />
    <ns:book3 />
    <otherns:newspaper1  />
    <ns:book4 />
  </ns:books>
</ns:bookstore>

book1,2,3 和 4 在 XML 中只能出现一次并且需要验证,其他命名空间中的元素然后 ns: 应该允许而不被验证.为此,我在 XSD 中使用了 xs:any 和 processContents 松懈:

book1,2,3 and 4 can only appear once in the XML and need to be validated, elements in other namespaces then ns: should be allowed without being validated. For this I use an xs:any with processContents lax in my XSD:

 <xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="ns">

<xs:element name="bookstore">
   <xs:complexType>
      <xs:sequence >
        <xs:element name ="book1" type="xs:string" maxOccurs="1"/>
        <xs:element name ="book2" type="xs:string" maxOccurs="1"/>
        <xs:element name ="book3" type="xs:string" maxOccurs="1"/>
        <xs:element name ="book4" type="xs:string" maxOccurs="1"/>
        <xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
       </xs:sequence>
    </xs:complexType>
</xs:element>

在这个方案中,其他命名空间中的元素只能出现在序列之后,不能出现在强制元素之间.理想的解决方案(但我知道在 XSD 中不允许这样做)是在 xs:all 中更改我的 xs:sequence(但 xs:any 在 xs:all 中不允许)

In this solution, the elements in other namespaces can only appear after the sequence, and not in between the obligatory elements. The ideal solution (but I know it's not allowed in XSD) is to change my xs:sequence in an xs:all (but xs:any is not allowed in xs:all)

我知道有这样的问题,但没有一个答案对我来说是清楚的.有人可以为我提供解决此问题的方法吗?

I know there are questions like this, but none of the answers there are clear to me. Can someone offer me a workaround for this problem?

我还尝试了以下方法:

 <xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="ns">

<xs:element name="bookstore">
   <xs:complexType>
      <xs:choice maxOccurs="unbounded" >
        <xs:element name ="book1" type="xs:string" maxOccurs="1"/>
        <xs:element name ="book2" type="xs:string" maxOccurs="1"/>
        <xs:element name ="book3" type="xs:string" maxOccurs="1"/>
        <xs:element name ="book4" type="xs:string" maxOccurs="1"/>
        <xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
       </xs:choice>
    </xs:complexType>
</xs:element>

因为选择的无界出现,选择中的任何元素都可以在任何位置出现任意多次.但是这里 book1、2、3 和 4 可以多次出现,在我的用例中是不允许的.

Because the unbounded occurs of the choice, any element from the choice can occur as many times as they want at any position. But here book1, 2, 3 and 4 can occur multiple times, with is not allowed in my use case.

有人有其他想法可以帮助我吗?提前致谢!!

Does anybody have another idea that might help me? Thanks in advance!!

P.S.:我的书的类型其实都是complexTypes,而且各不相同,这只是XML的简化版.

P.S.: The types of my books are in fact complexTypes and all different from eachother, this is just a simplified version of the XML.

这在我的 XSD 中也是不允许的:

This isn't allowed either in my XSD:

   <xs:element name="bookstore">
   <xs:complexType>
      <xs:sequence >
        <xs:element name ="book1" type="xs:string" maxOccurs="1"/>
        <xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name ="book2" type="xs:string" maxOccurs="1"/>
        <xs:any processContents="lax" namespace="##other" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name ="book3" type="xs:string" maxOccurs="1"/>
        <xs:element name ="book4" type="xs:string" maxOccurs="1"/>
        <xs:any processContents="lax" namespace="##other" minOccurs="0"    maxOccurs="unbounded"/>
       </xs:sequence>
    </xs:complexType>
</xs:element>

推荐答案

在 XSD 1.1 中,您可以通过定义一个开放的内容模型很容易地做到这一点:

In XSD 1.1 you can do this very easily by defining an open content model:

<xs:complexType ...>
  <xs:openContent mode="interleave">
    <xs:any namespace=.../>
  </xs:openContent>
  ... regular content model ...
</xs:complexType>

在 1.0 中没有简单的方法可以做到;你只需要在每个可能的位置放置一个可选的通配符.

There's no easy way to do it in 1.0; you just have to put an optional wildcard in every possible position.

这篇关于XSD:允许来自不同命名空间的元素在序列中的任何位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆