Java XML schema validation: prefix unbound

I have followed this tutorial to validate XML files However, an exception was received while validating the XML file What did I do wrong? My code:

<?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 file to validate:

<?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 source code for validation

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 I am receiving:

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

Solution

The XML schema file itself needs to be a valid XML document You are missing namespace declarations for external schema elements and XS prefixes

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- schema elements here -->
</xs:schema>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>