如何使用 xml 模式验证 xml?

发布于 2024-09-10 13:17:16 字数 3083 浏览 9 评论 0原文

是否可以使用以下模式验证以下 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();
        }

Is it possible to validate the following xml with the following schema?
I'd like to validate the xml without specifying the schema in the xml file.

I'm not sure if this is possible or not, but would appreciate some help figuring out how to do it.

I keep getting the following error when I attempt to validate the 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 Code I'm using to do the validation.

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();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

揽月 2024-09-17 13:17:16

您可以改为修改 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>

You can modify XML Schema instead. Remove targetNamespace from schema.

<?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>
烟沫凡尘 2024-09-17 13:17:16

可能是一个恼人的命名空间问题。元素 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>

Could be an annoying namespace problem. The element contact is defined in namespace http://madeupdomain.com/xml/contacts but the actual contacts element in the xml document is in the default namespace.

This could already remove the error:

<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>
活泼老夫 2024-09-17 13:17:16

http://www.edankert.com/apis/ jaxp.document-builder-factory.schema-validation.html

factory.setValidating(true)

编辑 - 没关系,我在第一次阅读时没有看到“未指定架构”;)

http://www.edankert.com/apis/jaxp.document-builder-factory.schema-validation.html

factory.setValidating(true)

edit -- never mind, i didnt see the "without specifying the schema" in my first read ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文