java - XML - XSD 的相对路径

标签 java xml xsd

我有一个简单的 Spring 项目,分为三个模块,例如:

  • 模块1
    • src/main/java/com/test/Module1Test.xml
  • 模块2
    • src/main/java/com/test/Module2Test.xml
    • src/main/java/com/test/Modules.xsd
  • 模块3
    • src/main/java/com/test/Module3Test.xml

就像您在 module2 中看到的那样,我创建了 XSD 文件来验证所有三个模块中的 XML 文件。 为了使用 module2 中的 XSD 验证 XML 文件,我只需添加以下行:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Modules.xsd">

但是我不知道如何使用 XSD 验证 Module1Test.xmlModule3Test.xml 文件。我无法在 XML 文件中使用 XSD 的绝对路径,例如:

<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:\\project\\module2\\src\\main\\java\\com\\test\\Modules.xsd">

我想使用相对路径,例如:

<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="project\\module2\\src\\main\\java\\com\\test\\Modules.xsd">

或者以其他更好的方式,只是不使用绝对路径。

你知道这是否可能吗?怎么解决呢?

最佳答案

我认为“这确实取决于”,取决于您的编码方式和引用方式,因此我会重点介绍这两种方法:

方法#1:作为直接流传递

您可以将 XSD 作为输入流直接传递到程序,这样即使 XML 文档中的元素没有命名空间,也无需依赖“noNamespaceSchemaLocation”属性。

下面是一个示例程序:

import org.w3c.dom.Document; 
import org.xml.sax.SAXException; 
import org.xml.sax.SAXParseException; 
import org.xml.sax.InputSource; 


import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.StringReader; 


import javax.xml.XMLConstants; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.transform.Source; 

import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 
import javax.xml.validation.Validator; 

public class XmlSchemaValidationHelper { 

    public static void main(String[] argv) {
        XmlSchemaValidationHelper schemaValidationHelper = new XmlSchemaValidationHelper();
        schemaValidationHelper.validateAgainstSchema(new File(argv[0]), new File(argv[1]));
    }

    public void validateAgainstSchema(File xmlFile, File xsdFile) { 
        try {
            System.out.println("### Starting...");
            // parse an XML document into a DOM tree 
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 
            builderFactory.setNamespaceAware(true); 
            DocumentBuilder parser = builderFactory.newDocumentBuilder(); 
            Document document = parser.parse(xmlFile); 

            // 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(xsdFile); 
            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 
            validator.validate(new DOMSource(document));
            System.out.println("### Finished...");

        } catch (FileNotFoundException ex) { 
            throw new OpenClinicaSystemException("File was not found", ex.getCause()); 
        } catch (IOException ioe) { 
            throw new OpenClinicaSystemException("IO Exception", ioe.getCause()); 
        } catch (SAXParseException spe) { 
            spe.printStackTrace(); 
            throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause()); 
        } catch (SAXException e) { 
            throw new OpenClinicaSystemException(e.getMessage(), e.getCause()); 
        } catch (ParserConfigurationException pce) { 
            throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause()); 
        } 
    } 

    public class OpenClinicaSystemException extends RuntimeException { 
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String errorCode; 
        private Object[] errorParams; 

        public OpenClinicaSystemException(String code, String message) { 
            this(message); 
            this.errorCode = code; 
        } 

        public OpenClinicaSystemException(String code, String message, Throwable cause) { 
            this(message, cause); 
            this.errorCode = code; 
        } 

        public OpenClinicaSystemException(String message, Throwable cause) { 
            super(message, cause); 
        } 

        public OpenClinicaSystemException(Throwable cause) { 
            super(cause); 
        } 

        public OpenClinicaSystemException(String message) { 
            super(message); 
            this.errorCode = message; 
        } 

        public OpenClinicaSystemException(String code, Object[] errorParams) { 
            this.errorCode = code; 
            this.errorParams = errorParams; 
        } 

        public OpenClinicaSystemException(String code, Object[] errorParams, String message) { 
            this(message); 
            this.errorCode = code; 
            this.errorParams = errorParams; 
        } 

        public String getErrorCode() { 
            return errorCode; 
        } 

        public Object[] getErrorParams() { 
            return errorParams; 
        } 

        public void setErrorParams(Object[] errorParams) { 
            this.errorParams = errorParams; 
        } 
    }

}

像这样运行:E:\xmlValidator>java XmlSchemaValidationHelper po.xml test/po.xsd 并且您不需要依赖“noNamespaceSchemaLocation”属性,因为您直接传递 XSD作为 validator/解析器的输入流。

这是根据 XSD 验证 XML 的快速但肮脏的方法

方法#2:正确转义 XSD 路径

您只需要正确转义 XSD 路径,因此请使用以下任一选项:

<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C://project//module2//src//main//java//com//test//Modules.xsd">

或者

<Module2Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C://project//module2//src//main//java//com//test//Modules.xsd">

阅读MSDN official article差不多。

<小时/>

最后一句话:使用 XML 命名空间和 targetNamespace

我建议您应该使用 XML 命名空间和 targetNamespace 来正确地将 XML 实例与 XML 架构相关联。考虑下面的示例 XSD 和 XML。

对于任何不理解 XML 命名空间和 targetNamespace 概念的人,我希望以下要点会有所帮助:

  • 在 XSD 中,您需要像这样定义“targetNamespace”targetNamespace="http://www.books.org",它只不过是为实际 XML 定义命名空间实例。您需要在 XML 实例中使用相同的命名空间,即 http://www.books.org
  • 在 XML 实例中,您需要:
    • 将 XSD 中的“targetNamespace”设置为默认命名空间,如下所示 xmlns="http://www.books.org"
    • 然后使用“schemaLocation”属性告诉 XML validator XML 模式中的目标命名空间应该是什么, validator 将适本地取消引用 URI 的 xsi:schemaLocation="http://www.books.组织”

示例 XML:

<?xml version="1.0"?>
<BookStore xmlns="http://www.books.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.books.org">
        <Book>
                <Title>My Life and Times</Title>
                <Author>Paul McCartney</Author>
                <Date>1998</Date>
                <ISBN>1-56592-235-2</ISBN>
                <Publisher>McMillin Publishing</Publisher>
        </Book>
        <Book>
                <Title>Illusions The Adventures of a Reluctant Messiah</Title>
                <Author>Richard Bach</Author>
                <Date>1977</Date>
                <ISBN>0-440-34319-4</ISBN>
                <Publisher>Dell Publishing Co.</Publisher>
        </Book>
        <Book>
                <Title>The First and Last Freedom</Title>
                <Author>J. Krishnamurti</Author>
                <Date>1954</Date>
                <ISBN>0-06-064831-7</ISBN>
                <Publisher>Harper &amp; Row</Publisher>
        </Book>
</BookStore>

示例 XSD:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.books.org"
            xmlns="http://www.books.org"
            elementFormDefault="qualified">
    <xsd:simpleType name="ISBN-type">
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}|\d{1}-\d{3}-\d{5}-\d{1}|\d{1}-\d{2}-\d{6}-\d{1}"/>
        </xsd:restriction>
    </xsd:simpleType>
    <xsd:element name="BookStore">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Book" maxOccurs="unbounded">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="Title" type="xsd:string"/>
                            <xsd:element name="Author" type="xsd:string"/>
                            <xsd:element name="Date" type="xsd:gYear"/>
                            <xsd:element name="ISBN" type="ISBN-type"/>
                            <xsd:element name="Publisher" type="xsd:string"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

关于java - XML - XSD 的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41433044/

相关文章:

c# - ArrayOfXElement 到 C# 中的数据集

java - JAXB:编码后替换 namespace

java - Spring Controller /组件实现可序列化

xml - 从位于不同项目模块中的不同 XML 引用 XSD 文件?

java - 有没有办法在 Spring WS 2 中公开静态 XSD?

php - xpath php基于大于日期的排序属性

javascript - XML 到 JSON 在 firefox 中有效,但在 chrome 中产生 TypeError

java - 自版本 4.3.2 以来未映射 hibernate 实体

java - 如何在没有CMD窗口的情况下运行jlink生成的Java运行时镜像?

java - 大量源文件的正确文件夹结构