如何使用XML模式验证XML?

3

是否可以使用以下模式验证以下xml? 我想在不在xml文件中指定模式的情况下验证xml。

我不确定是否可能,但是希望能帮助我弄清楚如何做到这一点。

当我尝试验证xml时,我一直收到以下错误。

org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'contacts'.

<?xml version="1.0" ?>
<contacts>
    <contact>
        <names>Joe Buddah</names>
        <address>123 Black Jack Cove</address>
        <phone>555-555-1212</phone>
    </contact>
    <contact>
        <name>Ray Buddah</name>
        <address>123 Black Jack Cove</address>
        <phone>555-555-1212</phone>
    </contact>
</contacts>

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://madeupdomain.com/xml/contacts" xmlns:tns="http://madeupdomain.com/contacts" elementFormDefault="qualified">
    <complexType name="contactType">
        <sequence>
            <element name="name" type="string" maxOccurs="1" minOccurs="1"></element>
            <element name="address" type="string"></element>
            <element name="phone" type="string"></element>
        </sequence>
    </complexType>
    <complexType name="contactsType">
        <sequence>
            <element name="contact" type="tns:contactType" minOccurs="0" maxOccurs="unbounded"></element>
        </sequence>
    </complexType>
    <element name="contacts" type="tns:contactsType"></element>
</schema>

我正在使用的Java代码进行验证。

static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
    {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, new File("src/main/resources/contacts.xsd"));

        // parse an XML document into a DOM tree
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();

        Document document = parser.parse(new File("src/main/resources/contacts.xml"));

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(new File("src/main/resources/patient.xsd"));
        Schema schema = factory.newSchema(schemaFile);

        // create a Validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();

        // validate the DOM tree
        try
        {
            validator.validate(new DOMSource(document));
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
3个回答

2

可能是一个令人烦恼的命名空间问题。元素contact在命名空间http://madeupdomain.com/xml/contacts中定义,但实际上XML文档中的contacts元素在默认命名空间中。

这可能已经解决了错误:

<contacts xmlns="http://madeupdomain.com/xml/contacts">
    <contact>
        <names>Joe Buddah</names>
        <address>123 Black Jack Cove</address>
        <phone>555-555-1212</phone>
    </contact>
    <contact>
        <name>Ray Buddah</name>
        <address>123 Black Jack Cove</address>
        <phone>555-555-1212</phone>
    </contact>
</contacts>

1
我的问题是,我能否在不更改XML文件的情况下使用XML模式验证我发布的XML。当我对XML文件进行更改时,它确实可以验证。 - ScArcher2
1
你已经拥有了DOM - 只需访问所有元素并将元素的命名空间设置为http://madeupdomain.com/xml/contacts。那也应该可以工作。在验证文档之前,元素节点contacts必须处于正确的命名空间中。 - Andreas Dolk

2
您可以修改XML Schema。从模式中删除targetNamespace。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="contactType">
      <xsd:sequence>
         <xsd:element name="name" type="xsd:string" maxOccurs="1" minOccurs="1"/>
         <xsd:element name="address" type="xsd:string"/>
      <xsd:element name="phone" type="xsd:string"/>
   </xsd:sequence>
   </xsd:complexType>
   <xsd:complexType name="contactsType">
      <xsd:sequence>
         <xsd:element name="contact" type="contactType" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="contacts" type="contactsType"/>
</xsd:schema>

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接