Java 文件路径中的特殊字符导致XML XSD验证失败

Java 文件路径中的特殊字符导致XML XSD验证失败,java,xml,xsd,xsd-validation,Java,Xml,Xsd,Xsd Validation,我在S.O.读到了很多关于这个错误的问题,但没有一个像我遇到的问题 在Eclipse中,我的项目通过以下路径导入: D:\repositoório\branch 7.x.x dev\project commons example.xsd位于文件夹\src\main\resources\schemas\中,包含一个包含另一个xsd的标记,如: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:ds="http://ww

我在S.O.读到了很多关于这个错误的问题,但没有一个像我遇到的问题

在Eclipse中,我的项目通过以下路径导入:

D:\repositoório\branch 7.x.x dev\project commons

example.xsd位于文件夹
\src\main\resources\schemas\
中,包含一个包含另一个xsd的标记,如:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.teste.com/abc" targetNamespace="http://www.teste.com/abc" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:include schemaLocation="layoutBase.xsd"/>
    <xs:element name="complexExample" type="CplExmpl">
        <xs:annotation>
            <xs:documentation> Process...</xs:documentation>
        </xs:annotation>
    </xs:element>
</xs:schema>
但是,当我运行服务并通过example.xsd进行验证时,它不起作用

以下是stacktrace:

Caused by: org.xml.sax.SAXParseException; systemId: file:/D:/Repositório/branch 7.x.x dev/project-commons/target/classes/schemas/example.xsd; 
    lineNumber: 4; columnNumber: 45; src-resolve: Cannot resolve the name 'CplExmpl' to a(n) 'type definition' component.
    at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseGlobal(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseSchemas(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:627)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:659)
    at br.com.oobj.util.xml.JAXPValidate$ValidatorKeyedPoolFactoryAdapter.getSchema(JAXPValidate.java:523)
    at br.com.oobj.util.xml.JAXPValidate$ValidatorKeyedPoolFactoryAdapter.makeObject(JAXPValidate.java:509)
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.borrowObject(GenericKeyedObjectPool.java:1212)
    at br.com.oobj.util.xml.JAXPValidate.borrowValidator(JAXPValidate.java:212)
    ... 12 more
之后,我在该服务的另一个目录中运行了一个测试。而且它起作用了。 错误似乎是由D:\repositoório中的特殊字符“ó”引起的。 未找到
。 我测试将complexType放入example.xsd中,它也可以工作

这只发生在我的环境中。它在我团队其他成员的环境中运行良好


有人已经遇到过这样的问题吗?

在项目的属性中打开选项卡“资源”,然后选择UTF-8的“文本文件编码”。

重现问题 我可以确认您报告的在Windows 7下运行Xerces-J v2.11.0的行为

我已将下面的代码修改为该问题的完整示例,包括以下特定更改:

  • XML文档的使用
    xsi:schemaLocation=”http://www.teste.com/abc 示例.xsd“
    提示 至于主XSD的位置
  • 主XSD的
    xmlns:abc=”声明http://www.teste.com/abc“
    并使用
    abc
    前缀来引用
    complexExample
    as
    abc:CplExmpl
  • XML文档:
    
    建议描述了Java类处理路径名中特殊字符的弱点。Xerces-J可能正被这些问题所困扰


    解决问题 使用完整URI表示法为所有文件指定绝对路径可以解决此问题:

  • 使用
    file:///D:/path/to/file.xml
  • 使用指定主XSD的位置
    xsi:schemaLocation=”http://www.teste.com/abc
    file:///D:/path/to/file/example.xsd“
  • 使用
    指定包含的XSD的位置
  • 进行这些更改将允许根据XSD验证XML文档,即使路径中存在特殊字符

    Caused by: org.xml.sax.SAXParseException; systemId: file:/D:/Repositório/branch 7.x.x dev/project-commons/target/classes/schemas/example.xsd; 
        lineNumber: 4; columnNumber: 45; src-resolve: Cannot resolve the name 'CplExmpl' to a(n) 'type definition' component.
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
        at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
        at org.apache.xerces.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
        at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(Unknown Source)
        at org.apache.xerces.impl.xs.traversers.XSDElementTraverser.traverseGlobal(Unknown Source)
        at org.apache.xerces.impl.xs.traversers.XSDHandler.traverseSchemas(Unknown Source)
        at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
        at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
        at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
        at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:627)
        at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:659)
        at br.com.oobj.util.xml.JAXPValidate$ValidatorKeyedPoolFactoryAdapter.getSchema(JAXPValidate.java:523)
        at br.com.oobj.util.xml.JAXPValidate$ValidatorKeyedPoolFactoryAdapter.makeObject(JAXPValidate.java:509)
        at org.apache.commons.pool.impl.GenericKeyedObjectPool.borrowObject(GenericKeyedObjectPool.java:1212)
        at br.com.oobj.util.xml.JAXPValidate.borrowValidator(JAXPValidate.java:212)
        ... 12 more
    
    <?xml version="1.1" encoding="UTF-8"?>
    <complexExample xmlns="http://www.teste.com/abc"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://www.teste.com/abc example.xsd"
                    version="1">
      <a/>
      <b/>
    </complexExample>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:abc="http://www.teste.com/abc"
               targetNamespace="http://www.teste.com/abc"
               elementFormDefault="qualified"
               attributeFormDefault="unqualified">
      <xs:include schemaLocation="layoutBase.xsd"/>
      <xs:element name="complexExample" type="abc:CplExmpl">
        <xs:annotation>
          <xs:documentation> Process...</xs:documentation>
        </xs:annotation>
      </xs:element>
    </xs:schema>
    
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:abc="http://www.teste.com/abc"
               targetNamespace="http://www.teste.com/abc"
               elementFormDefault="qualified"
               attributeFormDefault="unqualified">
      <xs:complexType name="CplExmpl">
        <xs:annotation>
          <xs:documentation> Process...</xs:documentation>
        </xs:annotation>
        <xs:sequence>
          <xs:element name="a"/>
          <xs:element name="b"/>
        </xs:sequence>
        <xs:attribute name="version"/>
      </xs:complexType>
    </xs:schema>