本文介绍了Java XML Schema验证:前缀未绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照来验证XML文件。但是我在验证XML文件时收到异常。我做错了什么?我的代码:

XML架构:

I have followed this tutorial for validating XML files. But I am receiving exception when validating XML file. What I am doing wrong? My codes:
XML schema:

<?xml version="1.0" encoding="utf-8" ?>

<!-- definition of simple elements -->
<xs:element name="first_name" type="xs:string" />
<xs:element name="last_name" type="xs:string" />
<xs:element name="phone" type="xs:string" />

<!-- definition of attributes -->
<xs:attribute name="type" type="xs:string" use="required"/>
<xs:attribute name="date" type="xs:date" use="required"/>

<!-- definition of complex elements -->

<xs:element name="reporter">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="first_name" />
            <xs:element ref="last_name" />
            <xs:element ref="phone" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="report">
    <xs:complexType>
        <xs:attribute ref="type"/>
        <xs:attribute ref="date" />
        <xs:sequence>
            <xs:element ref="reporter" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

要验证的XML文件:

<?xml version="1.0" encoding="utf-8" ?>
<report type="5" date="2012-12-14">
    <reporter>
        <first_name>FirstName</firstname>
        <last_name>Lastname</lastname>
        <phone>+xxxxxxxxxxxx</phone>
    </reporter>
</report>

用于验证的Java源:

Java source for validating:

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
import java.io.*;

public class ProtocolValidator
{
    public static void main(String [] args) throws Exception
    {
        Source schemaFile = new StreamSource(new File("schema.xsd"));
        Source xmlFile = new StreamSource(new File("test_xml.xml"));

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(schemaFile);
        Validator validator = schema.newValidator();

        try{
            validator.validate(xmlFile);
            System.out.println(xmlFile.getSystemId() + " is valid");
        }
        catch (SAXException e)
        {
            System.out.println(xmlFile.getSystemId() + " is NOT valid");
            System.out.println("Reason: " + e.getLocalizedMessage());
        }
    }
}

我收到的例外情况:

Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/root/test/schema.xsd; lineNumber: 4; columnNumber: 50; The prefix "xs" for element "xs:element" is not bound.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)...


推荐答案

XML模式文件本身需要是有效的XML文档。您缺少 xs 前缀的外部架构元素和名称空间声明。

The XML schema file itself needs to be a valid XML document. You are missing the outer schema element and the namespace declaration for the xs prefix.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- schema elements here -->
</xs:schema>

这篇关于Java XML Schema验证:前缀未绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:02