2017-01-02 63 views
0

我已经简单的Spring项目三个模块分裂,如:XML - 相对路径XSD

  • 模块1
    • 的src/main/JAVA/COM /测试/ Module1Test.xml
  • 模块2
    • 的src /主/ JAVA/COM /测试/ Module2Test.xml
    • 的src/main/JAVA/COM /测试/ Modules.xsd
  • 单词数
    • 的src/main/JAVA/COM /测试/ Module3Test.xml

你一样可以在module2中看到我已经创建了XSD文件来验证所有三个模块中的XML文件。 要在模块2验证XML文件,XSD,我只是简单地添加以下行:

<?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文件。我不能使用绝对路径来我的XSD的XML文件,如:

<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"> 

或者其他更好的办法,只是不使用绝对路径。

你知道这有可能吗?我该如何解决它?

+0

你试过'XSI:no​​NamespaceSchemaLocation =“文件:/// C:/项目/模块2/src目录/主/ JAVA/COM /测试/ Modules.xsd“'? –

+0

是的,但我不想使用fulll文件路径到XSD文件的相对路径 – bontade

回答

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作为输入流的验证/解析器。

这是根据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"> 

OR

<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命名空间和目标名称

我会建议你应该使用XML命名空间和目标名称与XML模式与正确的XML实例。考虑下面的示例XSD和XML。

对于任何人谁不理解XML命名空间和目标名称的概念,我希望下面的快速分将是有益的:

  • 在XSD,您需要定义“目标名称”像这样targetNamespace="http://www.books.org"这没有什么,但是你正在为你的实际XML实例定义命名空间。您需要在您的XML实例中使用相同的名称空间,即http://www.books.org
  • 在XML实例,您需要:
    • 从XSD制作了“目标名称”像这样xmlns="http://www.books.org"
    • 默认的名称空间,然后使用“的schemaLocation”属性来告诉XML验证那又该在XML架构的目标命名空间,并验证将反引用的URI的适当xsi:schemaLocation="http://www.books.org"

示例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>