我追随两个架构。 master.xsd和child.xsd

  1. child.xsd由master.xsd导入。
  2. 主文件具有目标命名空间'pub'。
  3. 子文件没有这样的命名空间。
  4. 当我尝试使用master.xsd验证XML时,我收到错误

    org.xml.sax.saxparseexception:src-解析:无法将名称“作者”解析为(n)'元素声明'组件。

    我还尝试在master.xsd中使用,这次我得到类似的错误:

    org.xml.sax.saxparseexception:src-解析:无法解析名称'pub:author'到a(n)元声明'组件。

    虽然这被XMLSPY验证成功。

    以下是架构,调用代码和验证代码:

    master.xsd

    <?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>
    
    .

    child.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");
    
    .

    请告诉我在哪里出错??

有帮助吗?

解决方案 2

通过实现LSResourcerSolver来修复它。 没有找到child.xsd。

在这里寻找更多的分歧 https://stackoverflow.com/a/2342859/842210

其他提示

我会想象你想要两个模式可用,因此有类似的模式:

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

交替,您可以为模式提供自定义LSResourcerSolver。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top