繁体   English   中英

通过XSD以任意顺序允许任何数量的XML元素?

[英]Allow XML element in any order with any number of appearance via XSD?

我很难用这些约束编写XSD: 名称元素是必需的; 元素可以出现0次或多次; 类别元素是可选的。 我现在有这样的XSD:

<xs:element name="Detail" maxOccurs="unbounded" minOccurs="0"> <!-- 0 or more Details -->
  <xs:complexType>
    <xs:sequence>
      <xs:element type="xs:string" name="Name"/> <!-- Name element is required -->
      <xs:element type="xs:string" name="Value" maxOccurs="unbounded" minOccurs="0"/> <!-- 0 or more occurences of Value element -->
      <xs:element type="xs:string" name="Category" minOccurs="0" maxOccurs="1"/> <!-- optional (0 or 1) Category element -->
    </xs:sequence>
  </xs:complexType>
</xs:element>

问题是输入XML可能不遵循模式中的顺序。 xs:choicexs:都有外观约束,对我来说还不够好。

任何人都可以帮我解决这个问题,允许XML元素以任何顺序出现在任何次序中吗?

附上示例输入XML:

<Detail>
    <Category />
    <Name> Thanks </Name>
    <Value> 1 </Value>
    <Value> 2 </Value>
</Detail>

XSD 1.0

不可能。 要么强制执行订单,要么放宽发生约束。

XSD 1.1

可能,因为在XSD 1.1中, xs:all现在可能有@maxOccurs="unbounded"

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           elementFormDefault="qualified"
  vc:minVersion="1.1">
  <xs:element name="Detail"> 
    <xs:complexType>
      <xs:all>
        <xs:element type="xs:string" name="Name"/>
        <xs:element type="xs:string" name="Value" 
                    minOccurs="0" maxOccurs="unbounded" />
        <xs:element type="xs:string" name="Category" 
                    minOccurs="0" maxOccurs="1"/>
      </xs:all>
    </xs:complexType>
  </xs:element>
</xs:schema>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM