2011-06-03 59 views
1

搜索并试验,研究并且绞尽脑汁关于此事(昨晚梦见它)。XSD:不同类型的同名<elements>的随机顺序=不可能

尝试构建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> 

我能想出当这些数据不验证的最好的是乱序(他们会)

<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解决方案,唉,我担心这是不可能的。

任何人都可以证明我错了吗?这是我很长一段时间的第一个模式项目。

UPDATE

你知道吗,我想我会验证它的结构,读<数据>部分瓦特/ LINQ,并通过XSLT转换它

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

无论如何,这可能有理由这样做,因为工作与验证后的数据将在<标题<值>配对任何如何成为janky。

回答

1

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

通常,XML Schema更多的是关于结构和打字,它的约束能力非常有限。

+0

谢谢@lexicore对于这一点,我看了一下Schematron,但考虑到这个实例中的特定数据结构,似乎我的XLST技巧会更容易实现 - 话虽如此,使用Schematron.NET [MSDN文章] (http://msdn.microsoft.com/en-us/library/aa468554.aspx)可能证明是有用的,尽管我的验证添加了另一层复杂性。 – diZzyCoDeR 2011-06-06 14:54:21