Java:针对包含另一个 xsd 的 xsd 进行字符串 xml 验证

标签 java validation xsd include

更新 1:新的验证方法引发新错误,请参见下文。

更新2:我已经用xml spy 检查了xml文件。没有错误。

希望有人能看出我错在哪里。

我目前正在尝试针对包含另一个 xsd 的 xsd 验证 xml 文件。我不知道我的 xsds 或 java 代码是否有问题。我没有存储 xml/xsd 文件,我从服务器获取它们作为 base64 字符串。希望有人能帮忙。

我收到以下错误:

org.xml.sax.SAXParseException; lineNumber: 81; columnNumber: 104; src-resolve: Cannot resolve the name 'ServiceSpecificationSchema:ServiceIdentifier' to a(n) 'type definition' component.

-ServiceSpecificationSchema.xsd

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example.org/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN">

    <include schemaLocation="ServiceBaseTypesSchema.xsd"/>
    <element name="serviceSpecification" type="ServiceSpecificationSchema:ServiceSpecification">
        <unique name="serviceDataModelTypeKey">
            <selector xpath=".//xs:*"/>
            <field xpath="@name"/>
        </unique>
        <keyref name="serviceDataModelReturnValueTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey">
            <selector xpath=".//ServiceSpecificationSchema:returnValueType"/>
            <field xpath="ServiceSpecificationSchema:typeReference"/>
        </keyref>
        <keyref name="serviceDataModelParameterTypeTypeKeyRef" refer="ServiceSpecificationSchema:serviceDataModelTypeKey">
            <selector xpath=".//ServiceSpecificationSchema:parameterType"/>
            <field xpath="ServiceSpecificationSchema:typeReference"/>
        </keyref>
    </element>
    <complexType name="ServiceSpecification">
        <all>
            <element name="id" type="ServiceSpecificationSchema:ServiceIdentifier" minOccurs="1" maxOccurs="1"/>
.....

ServiceBaseTypeSchema:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd" targetNamespace="http://example/ServiceSpecificationSchema.xsd" version="1.0" xml:lang="EN">
...
    <simpleType name="ServiceIdentifier">
        <restriction base="string"/>
    </simpleType>
...
</schema>

Java validator

public void validate(String inputXml, String XSD, String XSD2)
        throws SAXException, IOException {
        try {
            byte[] decoded = Base64.decodeBase64(XSD);
            XSD = new String(decoded, "UTF-8");
            byte[] decoded2 = Base64.decodeBase64(XSD2);
            XSD2 = new String(decoded2, "UTF-8");

        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        Schema schema = factory.newSchema(new SAXSource[]
        {
        (new SAXSource(new InputSource(new StringReader(XSD)))),
        (new SAXSource(new InputSource(new StringReader(XSD2))))
        });

        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(new StringReader(inputXml)));
        } catch (SAXException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        }

}

我找到了另一种可能的解决方案,但仍然不起作用。使用以下方法,会抛出异常,并根据 xml 验证 xsd。异常的行取决于所使用的 xml。在底部的示例中,错误位于第 2 行。 -> 错误:找不到元素 'ServiceSpecificationSchema:serviceSpecification' 的声明

我认为:1.我的java代码有问题或者2.所有的xml文件都有问题。

新的 Java validator :

byte[] decoded = Base64.decodeBase64(XSD);
                byte[] decoded2 = Base64.decodeBase64(XSD2);

                SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

                InputStream impFis = new ByteArrayInputStream(decoded2);
                InputStream mainFis = new ByteArrayInputStream(decoded);

                Source main = new StreamSource(mainFis);
                Source imp = new StreamSource(impFis);
                Source[] schemaFiles = new Source[] {imp, main};
                Schema schema = factory.newSchema(schemaFiles);
                Validator validator = schema.newValidator();
                validator.validate(new StreamSource(new StringReader(inputXml))); 

示例 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<ServiceSpecificationSchema:serviceSpecification
        xmlns:ServiceSpecificationSchema="http://example.org/ServiceSpecificationSchema.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://example.org/ServiceSpecificationSchema.xsd">
.....

最佳答案

终于我找到了解决办法。标准方法不会从一开始就解析包含或导入的文件。为了解析包含的文件,我使用了 LSResourceResolver。

这对我有用:

@Service
public class ResourceResolverImpl implements LSResourceResolver {
private ILoadFromSRService iLoadFromSRService;

@Autowired
public ResourceResolverImpl(ILoadFromSRService iLoadFromSRService){
    this.iLoadFromSRService = iLoadFromSRService;
}

public LSInput resolveResource(String type,
                               String namespaceURI,
                               String publicId,
                               String systemId,
                               String baseURI) {
    String string =iLoadFromSRService.getServiceBaseTypeSchema();
    string = string.replace("\n", "").replace("\t", "");
    InputStream resourceAsStream = new ByteArrayInputStream( string.getBytes());
    return new LSInputImpl(publicId, systemId, resourceAsStream);
    }
}

关于Java:针对包含另一个 xsd 的 xsd 进行字符串 xml 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458397/

相关文章:

java - 使用 XSD 架构描述 'storeToXML ' 生成的 Java Properties

java - 使用 Hibernate 和 SQL Server 2008 的问题

java - 当尝试操作 ArrayList 时,复制 ArrayList 还是使用 ListIterator 更快?

java - 如何在Spring中更改Application.properties的值

java - 在 onBindAndValidate 中将值放入 error.getModel 中

xml - 可以更好地控制 xsl/xpath 中的 for-each 吗?

java - 从 Android 扬声器播放声音

javascript - 如何在 asp.net mvc 中的客户端验证后运行脚本

javascript - HTML5/JS 约束验证 Bug

java - jaxb可以生成这样的xml模式吗?