0

I have the following XML document structure that is already in production and cannot be changed:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<root xmlns="http://www.domain_name.com/MySchema">
    <optionalString1>a</optionalString1>
    <optionalString2>b</optionalString2>
    <optionalDependee1>c</optionalDependee1>
    <dependent1_1>d</dependent1_1>
    <dependent1_2>e</dependent1_2>
    <optionalString3>f</optionalString3>
    <optionalDependee2>g</optionalDependee2>
    <dependent2_1>h</dependent2_1>
    <dependent2_2>i</dependent2_2>
    <!-- more and more elements -->
</root>

Here are the restrictions I'm trying to express in XSD:

  1. XML elements can appear in any order.
  2. "optionalStringX" and "optionalDependeeX" may or may not be present in a given file.
  3. If "optionalDependeeX" IS present, then "dependentX_1" and "dependentX_2" are REQUIRED to be present. Otherwise, if "optionalDependeeX" IS NOT present, neither "dependentX_1" nor "dependentX_2" will be present.

I've tried several varations using 'complexType' and 'group' but they aren't valid XSD (at least according to VS2010). My 'group' implementation is like this:

<xs:schema>

<xs:group name="BaseGroup">
  <xs:all>
    <xs:element name="optionalString1" type="xs:string" minOccurs="0" />
    <xs:element name="optionalString2" type="xs:string" minOccurs="0" />
    <xs:element name="optionalString3" type="xs:string" minOccurs="0" />
  </xs:all>
</xs:group>

<xs:group name="DependentGroup1">
  <xs:all>
    <xs:element name="optionalDependee1" type="xs:string" minOccurs="1" />
    <xs:element name="dependent1_1" type="xs:string" minOccurs="1" />
    <xs:element name="dependent1_2" type="xs:string" minOccurs="1" />
  </xs:all>
</xs:group>

<xs:group name="DependentGroup2">
  <xs:all>
    <xs:element name="optionalDependee2" type="xs:string" minOccurs="1" />
    <xs:element name="dependent2_1" type="xs:string" minOccurs="1" />
    <xs:element name="dependent2_2" type="xs:string" minOccurs="1" />
  </xs:all>
</xs:group>

<xs:group name="Combo1">
  <xs:all>
    <!-- The 'http://www.w3.org/2001/XMLSchema:group'
         element is not supported in this context. -->
    <xs:group ref="BaseGroup"/>
    <xs:group ref="DependentGroup1"/>
  </xs:all>
</xs:group>

<xs:group name="Combo2">
  <xs:all>
    <!-- The 'http://www.w3.org/2001/XMLSchema:group'
         element is not supported in this context. -->
    <xs:group ref="BaseGroup"/>
    <xs:group ref="DependentGroup1"/>
    <xs:group ref="DependentGroup2"/>
  </xs:all>
</xs:group>

<xs:complexType name="RootType">
  <xs:choice minOccurs="1" maxOccurs="1">
    <xs:group ref="BaseGroup" />
    <xs:group ref="Combo1" />
    <xs:group ref="Combo2" />
  </xs:choice>
</xs:complexType>

<xs:element name="root" type="RootType" />

</xs:schema>

I'm new to schemas for validating XML and have always assumed until now that XSD was flexible enough to represent the structure of all valid XML. If I could rewrite the structure of the XML files, I'd make dependentX_1 and dependentX_2 either attributes or subelements of optionalDependeeX since those would express a dependent relationship.

  1. Can these restrictions be represented in an XSD schema? We're trying to stick with XSD if possible since it is my understanding that tools and support for it are alot more widespread than other validation solutions.
  2. If not, what other XML validator has good support in Python, Perl, and either C or C++ and can handle more complicated document structures than XSD? Would RelaxNG or Schematron be a good fit? Pros and cons? I've never used anything besides XSD.
4

2 回答 2

1

当您正确完成后,可以使用 xsd:all 放宽排序,但不支持您尝试执行的其他操作。XML Schema的下一个版本,即 1.1,可以支持您拥有的其他一些变体。但是 1.1 不是最终版本,没有多少库可以理解这一点。我建议你看看 schematron,看看是否有帮助。

于 2011-03-15T17:32:49.787 回答
1

直到现在一直认为 XSD 足够灵活,可以表示所有有效 XML 的结构

不,这与事实相去甚远——至少对于 XSD 1.0 而言。事实上,如果你想允许元素以任何顺序出现,除了说明哪些元素必须出现以及哪些是可选的之外,你可以施加的约束很少。

于 2011-03-15T21:29:52.163 回答