3

他のスキーマの要素、またはどのスキーマにもない要素に配置できる属性を定義する XSD を作成したいと考えています。たとえば、スキーマは次のようになります。

<xs:schema id="MySchema"
    targetNamespace="http://tempuri.org/MySchema"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/MySchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:attribute name="myAttribute" />
</xs:schema>

ドキュメントは次のようになります。

<someElement xmlns="http://tempuri.org/OtherSchema" xmlns:m="http://tempuri.org/MySchema">
  <someOtherElement someAttribute="value" m:myAttribute="value2" />
</someElement>

この例の「OtherSchema」は次のようになります。

<xs:schema id="OtherSchema"
    targetNamespace="http://tempuri.org/OtherSchema"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/OtherSchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="someElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded" name="someOtherElement">
          <xs:complexType>
            <xs:attribute name="someAttribute" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

検証を実行する C# コンソール アプリケーションを含む完全な例は、http://dl.getdropbox.com/u/407740/SchemaTest.zipからダウンロードできます。私の目標は、「OtherSchema」を変更せずにこれを検証することです。これは可能ですか?

4

4 に答える 4

1

スキーマを再定義して、好きなように拡張できます。このようにして、実際にファイルを変更することなく、既存のスキーマの定義を変更できます。ただし、要素を再定義できないため(complexTypesなどのみ 。http://www.w3.org/TR/xmlschema-1/#element-redefineを参照)、与えられた例では機能しません。したがって、私はあなたの例を明示的なcomplexTypesに分割したので、それらは再定義のために公開されます。

RedefineOtherSchema.xsd:

<xs:schema id="RedefineOtherSchema"
    targetNamespace="http://tempuri.org/OtherSchema"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/OtherSchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:m="http://tempuri.org/MySchema">          <!-- for the ref -->

  <xs:import schemaLocation="MySchema.xsd"
    namespace="http://tempuri.org/MySchema"/>       <!-- import -->

  <xs:redefine schemaLocation="OtherSchema.xsd">    <!-- redefine -->
    <xs:complexType name="SomeOtherElement">
      <xs:complexContent>
        <xs:extension base="SomeOtherElement">
          <xs:attribute ref="m:myAttribute" />      <!-- the ref -->
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>

OtherSchema:

<xs:schema id="OtherSchema"
    targetNamespace="http://tempuri.org/OtherSchema"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/OtherSchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="someElement" type="SomeElement"/>

    <xs:complexType name="SomeElement">
      <xs:sequence>
        <xs:element minOccurs="0" maxOccurs="unbounded"
                    name="someOtherElement" type="SomeOtherElement"/>
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="SomeOtherElement">
      <xs:attribute name="someAttribute" />
    </xs:complexType>
</xs:schema>

MySchema :(変更なし)

<xs:schema id="MySchema"
    targetNamespace="http://tempuri.org/MySchema"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/MySchema"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:attribute name="myAttribute"/>
</xs:schema>

なぜ<complexContent>ですか? 再定義は、既存のタイプの拡張(または制限)である必要があります。これにより、以前の定義が変更されます。拡張機能が含まれている必要があります<complexContent>(私は信じています)。

なぜ<import>ですか? xsd内の複数の名前空間で物事を定義することはできません(「targetNamespace」は1つだけです)。ただし、別のxsdから定義をインポートすることでこれを回避できます(その場合、定義を「定義」していません)。[別の方法はありますか?]

HTH :-)

于 2009-06-03T18:38:04.550 に答える
0

これは、まさに NVDL (名前空間ベースの検証およびディスパッチ言語) が提供するものです。複数のスキーマ/語彙を組み合わせて、それらのスキーマを変更することなくドキュメントを検証できます。NVDL は ISO 標準です。

あなたのケースを処理する有効な NVDL スクリプトを以下に示します。

<rules xmlns="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0" startMode="other">
  <mode name="other">
    <namespace ns="http://tempuri.org/OtherSchema">
      <validate schema="other.xsd" useMode="validateMyAttributes"/>
    </namespace>
  </mode>
  <mode name="validateMyAttributes">
    <namespace ns="http://tempuri.org/MySchema" match="attributes">
      <validate schema="my.xsd"/>
    </namespace>
  </mode>
</rules>

基本的には、other.xsd スキーマを使用して ...tempuri.org/OtherSchema 名前空間にあるものを検証し、my.xsd スキーマを使用して ...tempuri.org/MySchema の属性を検証します。

NVDL の詳細については、www.nvdl.org を参照してください。上記のスクリプトは、oNVDL でテストされています。

于 2009-06-04T20:46:26.273 に答える
-1

xsi:nil、xsi:schemaLocation、および xsi:noNamespaceSchemaLocation を検討してください。答えはイエスです。

また、それを試して確認するのにもそれほど時間はかかりませんでした。


スキーマに targetNamespace がありませんでした。これを試して:

<xs:schema xmlns="MySchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="MySchema">
  <xs:attribute name="myAttribute" />
</xs:schema>
于 2009-05-31T16:25:01.823 に答える