簡體   English   中英

Java:SAX架構驗證

[英]Java: SAX Schema validation

我遵循兩個模式。 Master.xsd和Child.xsd

  1. Child.xsd由Master.xsd導入。
  2. 主文件具有目標名稱空間“ pub”。
  3. 子文件沒有這樣的命名空間。

當我嘗試使用Master.xsd驗證xml時,出現錯誤

org.xml.sax.SAXParseException:src-resolve:無法將名稱“ author”解析為一個(n)“元素聲明”組件。

我也嘗試在master.xsd中使用,這一次我得到了類似的錯誤:

org.xml.sax.SAXParseException:src-resolve:無法將名稱“ pub:author”解析為一個(n)“元素聲明”組件。

盡管已成功通過XMLSpy驗證。

這是模式的調用代碼和驗證代碼:

大師版

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:pub="http://schema.abc.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schema.abc.com">
    <xs:import schemaLocation="Child.xsd"/>
    <xs:element name="books">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element ref="pub:book"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>
    <xs:element name="book">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="pub:published_date"/>
                <xs:element ref="author"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="published_date" type="xs:dateTime"/>
</xs:schema>

小孩.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="author">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="first_name"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="first_name" type="xsd:string"/>
</xsd:schema>

需要驗證的Sample.xml:

<?xml version="1.0" encoding="UTF-8"?>
<pub:books xsi:schemaLocation="http://schema.abc.com" xmlns:pub="http://schema.abc.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <pub:book>
        <pub:published_date>2001-12-17T09:30:47Z</pub:published_date>
        <author>
            <first_name>Adi</first_name>
        </author>
    </pub:book>
</pub:books>

用於驗證的Java代碼:

private void validate(final String schema, final String xml) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        InputStream is = getClass().getResourceAsStream(schema);
        Schema schema;
        try {
            schema = schemaFactory.newSchema(new StreamSource(is));
            Validator validator = schema.newValidator();
            Source xmlSource = new StreamSource( new StringReader(xml));

            validator.validate(xmlSource);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

調用代碼:

validate(masterXSDPath, "xmlString");

請告訴我我要去哪里錯了?

我想你會想要兩個模式都可用,因此像:

schemaFactory.newSchema(new Source[]{new StreamSource(is1), new StreamSource(is2)});

或者,您可以向SchemaFactory提供自定義的LSResourceResolver。

通過實現LSResourceResolver對其進行了修復。 找不到Child.xsd。

在這里查看更多細節https://stackoverflow.com/a/2342859/842210

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM