3

次の XML があります。

<?xml version="1.0" encoding="ISO-8859-1"?>
<library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="library.xsd">
<!--<!DOCTYPE library SYSTEM "library.dtd">-->

<authors>
  <author aid="a1">Bill Evjen</author>
  <author aid="a2">Michael Kay</author>
  <author aid="a3">Kevin Goldberg</author>
  <author aid="a4">Michael Morrison</author>
</authors>

<books>

<book bookID="b001" author="a2">
  <title>XSLT 2.0 and XPath 2.0 Programmer's Reference</title>
  <stock>4</stock>
  <publisher>John Wiley</publisher>
  <year>2009</year>
  <use type="advanced" />
  <use type="reference" />
</book>

<book bookID="b002" author="a1 a2">
  <title>Professional XML (Programmer to Programmer) </title>
  <stock>2</stock>
  <publisher>John Wiley</publisher>
  <year>2007</year>
  <use type="professional" />
  <use type="advanced" />
  <use type="reference" />
</book>

<book bookID="b003" author="a3">
  <title>XML: Visual QuickStart Guide</title>
  <stock>3</stock>
  <publisher>Peachpit Press</publisher>
  <year>2008</year>
  <use type="introductory" />
  <use type="reference" />
</book>

<book bookID="b004" author="a4">
  <title>Sams Teach Yourself XML in 24 Hours</title>
  <stock>5</stock>
  <publisher>SAMS</publisher>
  <year>2005</year>
</book>

</books>

</library>

book 要素の 'author' 属性に author 要素の 'aid' 属性の値/値のリストが含まれるように、xsd で key/keyref を使用するにはどうすればよいですか?

Visual Studio で作成した次の XSD をテストしましたが、「Keyref 'books' は参照されたキーまたはスコープ内の一意のキーを見つけることができません。」というエラーが表示されます。これを修正する方法がわかりません:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="authors">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="author" maxOccurs="unbounded" type="theAuthor"/>
            </xs:sequence>
          </xs:complexType>
          <xs:key name="uniqueAuthorID">
            <xs:selector xpath="author"/>
            <xs:field xpath="@aid"/>
          </xs:key>

        </xs:element>
        <xs:element name="books">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="book" type="theBook" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
          <xs:unique name="uniqueBookID">
            <xs:selector xpath="book"/>
            <xs:field xpath="@bookID"/>
          </xs:unique>
          <xs:keyref refer="uniqueAuthorID" name="authorKeyRef">
            <xs:selector xpath="book"></xs:selector>
            <xs:field xpath="@author"></xs:field>
          </xs:keyref>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>




  <xs:complexType name="theAuthor">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="aid" type="xs:string">
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>



  <xs:complexType name="theBook">
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="stock" type="xs:integer"/>
      <xs:element name="publisher" type="xs:string"/>
      <xs:element name="year" type="xs:string"/>
      <xs:element name="use" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="type" type="useType" default="advanced"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="bookID" type="xs:string"/>
    <xs:attribute name="author" type="xs:string"/>
  </xs:complexType>

  <xs:simpleType name="useType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="advanced" />
      <xs:enumeration value="reference" />
      <xs:enumeration value="professional" />
      <xs:enumeration value="introductory" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
4

1 に答える 1

6

問題は、制約宣言のレイアウト/スコープにあります。下の図を見てみましょう。

ここに画像の説明を入力

「中かっこ」プログラミングの世界 (Java/C#) と同様に、要素は、宣言の名前を自動的にスコープする「ブロック」として機能します。この場合、 youruniqueAuthorIDは作成者以下に表示されます。これが、 の下に定義されているものに表示されない理由ですbooks

uniqueAuthorIDそのため、アンダーライブラリを移動して修正する必要があります。

ここに画像の説明を入力

これで XSD の問題が解決されます。

ただし、期待どおりに XML は検証されません。

The key sequence 'a1 a2' in Keyref fails to refer to some key.

author をリスト制約付き単純型として適切に定義したとしても、keyref メカニズムはそのコンポーネント値でそれを壊しません。これを機能させるには、author 属性を繰り返し要素に変換し、代わりに各要素に対して keyref をチェックする必要があります(念のため、属性を繰り返すことはできません)。

于 2013-04-13T16:43:19.023 に答える