2017-01-23 5 views
0

私のプロジェクトで既存のXMLファイルの一部を検証しようとしています。例えば、構造は次のようになります。すなわちユニークなパーティクルエラー、任意の順序で複数の子要素を許可したい

<resources> 
    <file name="one" path="C:\test\one.txt" /> 
    <cache path="C:\test\cache\" /> 
    <file name="two" path="C:\test\two.txt" /> 
    <bundle name="myFolder"> 
     <file name="three" path="C:\test\three.txt" /> 
     <file name="four" path="C:\test\four.txt" /> 
    </bundle> 
    <file name="one" path="C:\test\one.txt" /> 
    <bundle name="myFolder"> 
     <file name="three" path="C:\test\three.txt" /> 
     <file name="four" path="C:\test\four.txt" /> 
    </bundle> 
    <file name="one" path="C:\test\one.txt" /> 
</resources> 

、私が欲しいのは、任意の数の任意の順序

  • で、子どもたち

    • で最も1つのcache要素を持つルート要素resources、との構造であり、 fileおよびbundle要素の任意の順序で、
    • bundleには、任意の数のfile要素が含まれます。

    これは(this answerから描画)私の現在のXSDです:

    <?xml version="1.0" encoding="UTF-8"?> 
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> 
        <xs:complexType name="cache"> 
         <xs:attribute name="path" type="xs:string" /> 
        </xs:complexType> 
    
        <xs:complexType name="file"> 
         <xs:attribute name="name" type="xs:string" /> 
         <xs:attribute name="path" type="xs:string" /> 
        </xs:complexType> 
    
        <xs:complexType name="bundle"> 
         <xs:choice maxOccurs="unbounded"> 
          <xs:element name="file" type="file" maxOccurs="unbounded" /> 
         </xs:choice> 
         <xs:attribute name="type" /> 
         <xs:attribute name="name" /> 
        </xs:complexType> 
    
        <xs:group name="unboundednoncache"> 
         <xs:choice> 
          <xs:element name="file" type="file" /> 
          <xs:element name="bundle" type="bundle" /> 
         </xs:choice> 
        </xs:group> 
    
        <xs:element name="resources"> 
         <xs:complexType> 
          <xs:sequence> 
           <!-- Validates if this next line is removed, 
           and the cache element is moved to first --> 
           <xs:group ref="unboundednoncache" minOccurs="0" maxOccurs="unbounded" /> 
           <xs:element name="cache" type="cache" minOccurs="0" maxOccurs="1" /> 
           <xs:group ref="unboundednoncache" minOccurs="0" maxOccurs="unbounded" /> 
          </xs:sequence> 
         </xs:complexType> 
        </xs:element> 
    </xs:schema> 
    

    これは私にエラーを与える:

    Cos-nonambig: File And File (or Elements From Their Substitution Group) Violate "Unique Particle Attribution". During Validation Against This Schema, Ambiguity Would Be Created For Those Two Particles.

    私が最初<xs:group>を削除する場合、私は検証する最初のXSDを取得することができますXSDでcache要素をXMLの最初の子に移動しますが、キャッシュ要素はどこでも有効です。

    (これより前のバージョンに私が使っていた:私はどちらかしたくない複数のキャッシュ・エレメントを、

    <xs:choice maxOccurs="unbounded"> 
        <xs:element name="cache" type="cache" minOccurs="0" maxOccurs="1" /> 
        <xs:element name="file" type="file" minOccurs="0" maxOccurs="unbounded" /> 
        <xs:element name="bundle" type="bundle" minOccurs="0" maxOccurs="unbounded" /> 
    </xs:choice> 
    

    ..しかし、それはできます)

    はなぜ私のXSDは違反しない「ユニークな粒子私はそれをどのように修正することができますか?あなたのスキーマ文書で

  • 答えて

    2

    、あなたが

    ((file | bundle)*, cache?, (file | bundle)*) 
    

    の同等としてresourcesのコンテンツモデルを書かれているこれは意味的に正しいですが、初期file要素は、中fileの第二の発生の最初のいずれかに一致する可能性がコンテンツモデル最善を尽くしていない理由は、XSDでは許可されていません。

    したがって、確定的な同等のコンテンツモデルが必要です。いないすべての非決定的内容モデルは決定論的同等物を持っていますが、あなたのことを行います

    ((file | bundle)*, (cache, (file | bundle)*)?) 
    

    あるいは、XSD構文で(unboundednoncacheのあなたの定義を再利用):

    <xs:group name="cache-plus-noncache"> 
        <xs:sequence> 
        <xs:element name="cache" type="cache" 
           minOccurs="1" maxOccurs="1" /> 
        <xs:group ref="unboundednoncache" 
           minOccurs="0" maxOccurs="unbounded" /> 
        </xs:sequence> 
    </xs:group> 
    
    <xs:element name="resources"> 
        <xs:complexType> 
        <xs:sequence> 
         <xs:group ref="unboundednoncache" 
           minOccurs="0" maxOccurs="unbounded" /> 
         <xs:group ref="cache-plus-noncache" 
           minOccurs="0" maxOccurs="1" /> 
        </xs:sequence> 
        </xs:complexType> 
    </xs:element> 
    

    ツールを持っていると便利かもしれません決定論的違反(「ユニークなパーティクル帰属」とも呼ばれる)ルールを読み取って、決定論的な同等物を提案するか、またはコンテンツモデルが決定論的に等価でないという悪いニュースを破った。この理論は、AnneBrüggemann-Kleinによってきれいに処理されています。しかし、これまでのところ、私はそのような道具を知らないので、私の定期的な執筆決議はこれまで果実を成し遂げていませんでした。

    関連する問題