0

私は次のXMLファイルを持っています:

<?xml version="1.0"?>
<!DOCTYPE library SYSTEM "library.dtd">
<library
xmlns="http://example.com/a"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com library.xsd"
name=".NET Developer's Library">
<book>
<category>computerss</category>
<title>Programming Microsoft .NET</title>
<author>Jeff Prosise</author>
<isbn>0-7356-1376-1</isbn>
</book>
<book>
<category>computer</category>
<title>Microsoft .NET for Programmers</title>
<author>Fergal Grimes</author>
<isbn>1-930110-19-7</isbn>
</book>
</library>

そして、次のJavaコード:

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
SchemaFactory sf = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
docBuilderFactory.setSchema(sf.newSchema(new File("library.xsd")));
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.parse(new FileInputStream("data.xml"));

次のエラーが発生します。

[Error] :7:33: cvc-elt.1: Cannot find the declaration of element 'library'.

XMLファイルのXSD宣言を削除すると、すべて正常に機能します...

内部は高く評価されています。ありがとう。

そして、ここにスキーマがあります:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="library">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="book" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:attribute name="name" type="xs:string" use="optional"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="category"/>
                <xs:element ref="title"/>
                <xs:element ref="author"/>
                <xs:element ref="isbn"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element  name="category">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="computer" />
            <xs:enumeration value="poetry" />
        </xs:restriction>
    </xs:simpleType>
    </xs:element>
    <xs:element name="title" type="xs:string"/>
    <xs:element name="author" type="xs:string"/>
    <xs:element name="isbn" type="xs:string"/>
</xs:schema>
4

2 に答える 2

2

XMLには、スキーマと同じではない名前空間(xmlns = "http://example.com/a")への参照があります。XMLエディター(AltovaやEclipseなど)のスキーマに対してXMLを検証しようとしましたか?

これまでのところ、解析エラーは正当であるように見えますが、スキーマによればXMLは無効です。

于 2012-04-18T14:22:59.317 に答える
0

スキーマ定義が正しくありません。まず、maximdimが言うように、スキーマtargetNamespace="http://mysite.com/a"にはスキーマタグに属性がありません。

次に、スキーマにはルート要素が1つしかないように見えますが、スキーマには6つあります。

XMLインスタンスの正しいスキーマは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://mysite.com/a" targetNamespace="http://mysite.com/a">

  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" type="book" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="book">
    <xs:sequence>
      <xs:element name="category">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:enumeration value="computer" />
            <xs:enumeration value="poetry" />
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="author" type="xs:string"/>
      <xs:element name="isbn" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
于 2012-04-18T14:44:20.023 に答える