【问题标题】:XSD: random order of same-named <elements> with different types = IMPOSSIBLEXSD:具有不同类型的同名 <elements> 的随机顺序 = 不可能
【发布时间】:2011-06-03 19:50:51
【问题描述】:

为此搜索、实验、研究和绞尽脑汁(昨晚梦到它)。

尝试构建一个 XSD 架构,以根据标签的值来验证以下示例 XML。

<data>
  <dataSet>
    <title>mediaType</title>
    <value>FullLength</value>
  </dataSet>
  <dataSet>
    <title>available</title>
    <value>true</value>
  </dataSet>
  <dataSet>
    <title>country</title>
    <value>Canada</value>
  </dataSet>
</data>

关于对单个数据集的约束的架构

<xs:complexType name="typeAvailable">
    <xs:sequence>
        <xs:element name="title">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="available" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="value">
            <xs:simpleType>
                <xs:restriction base="xs:boolean" />
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="typeMediaType">
    <xs:sequence>
        <xs:element name="title">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="mediaType" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="value">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="FullLength|Clip" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="typeCountry">
    <xs:sequence>
        <xs:element name="title">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="typeCountry" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        <xs:element name="value">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="Canada|US" />
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
    </xs:sequence>
</xs:complexType>

我能想到的最好的方法不会验证 dataSet 何时出现故障(它们将是)

<xs:complexType name="typeData">
    <xs:all>
        <xs:element name="dataSet" type="typeMediaType" />
        <xs:element name="dataSet" type="typeAvailable" />
        <xs:element name="dataSet" type="typeCountry" />
    </xs:all>
</xs:complexType>

当然,我对我得到的数据感到困惑,但没有什么说我不能用 XSLT 将它转换成什么,我不知道。我希望有一个优雅的 XSD 解决方案,唉,我担心这是不可能的。

谁能证明我错了?这是我很长一段时间以来的第一个架构项目。

更新

你知道吗,我想我要验证它的结构,读取带有 LINQ 的 部分并通过 XSLT 将其转换为

<data>
    <mediaType>FullLength</mediaType>
    <available>true</available>
    <country>Canada</country>
</data>

无论如何我都应该这样做,因为在验证后处理数据在

<value> 对中无论如何都会很麻烦。 </value>

【问题讨论】:

    标签: xml xsd xml-validation


    【解决方案1】:

    “条件”约束不适用于 XML 模式。键的技巧很少,但它们非常有限。考虑使用 Schematron,它具有更丰富的验证功能。

    一般来说,XML Schema 更多的是结构和类型,它的约束能力非常有限。

    【讨论】:

    • 感谢@lexicore,我看了一点 Schematron,但考虑到本实例中的特定数据结构,我的 XLST 技巧似乎更容易实现——话虽如此,使用 Schematron。 NET MSDN article 可能会被证明是有用的,尽管在我的验证中增加了另一层复杂性。
    【解决方案2】:

    Element Declarations Consistent 约束不允许具有不同类型的同名元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 2020-01-23
      相关资源
      最近更新 更多