2010-07-15 191 views
3

是否可以使用以下模式驗證以下xml? 我想驗證xml而不指定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(); 
     } 

回答

2

您可以修改XML架構來代替。從模式中刪除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

修復它。謝謝!! – ScArcher2 2010-07-16 13:15:22

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 2010-07-15 21:50:44

+1

您已經擁有了dom - 只需訪問所有元素並將元素的名稱空間設置爲「http:// madeupdomain.com/xml/contacts」。這也應該起作用。驗證文檔之前,元素節點「contacts」必須位於正確的名稱空間中。 – 2010-07-15 22:00:34