8

问题:我们有几个通过 XSLT 生成大量 XML 的服务。我们没有任何 XSD。我已经花时间创建 XSD 并想确认它们是正确的。目前我正在尝试验证 XSD 和 XML 是否正确验证。

问题:我有一个导入所有 xsd 的 xsd(common.xsd)。它还没有公开托管,所以直到最近我才发现将 common.xsd 的完整路径放在 AccountList.xsd 中,我能够走得更远。我现在收到以下内容:

org.xml.sax.SAXParseException;行号:9;列号:70;s4s-att-invalid-value:元素“元素”中的“类型”属性值无效。记录的原因:UndeclaredPrefix:无法将“common:response”解析为 QName:未声明前缀“common”。

我很茫然。我找不到在论坛中提出的示例或成功的源代码片段。我将不胜感激任何帮助,以便成功验证我的 xml。

常见的.xsd

<xs:schema  version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"
        xmlns="http://www.myorg.com/xsd/gen_fin"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        targetNamespace="http://www.myorg.com/xsd/gen_fin">
    <xs:complexType name="response">
        <xs:sequence>
            <xs:element name="code" type="xs:string"/>
            <xs:element name="description" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

AccountList.xsd

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>

<xs:schema  version="1.0" elementFormDefault="qualified" attributeFormDefault="unqualified"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://www.myorg.com/xsd/accList"
            targetNamespace="http://www.myorg.com/xsd/accList"
            xmlns:common="http://www.myorg.com/xsd/gen_fin">
    <xs:import namespace="http://www.myorg.com/xsd/gen_fin" 
               schemaLocation="/home/me/dev/projects/svn/myorg/xsd/src/main/resources/bg/gen_resp/common.xsd"/>

    <xs:element name="fundamo">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="response" type="common:response" minOccurs="1" maxOccurs="1"/>
                <xs:element name="transaction" type="tns:transaction" minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="transaction">
        <xs:sequence>
            <xs:element name="transactionRef" type="xs:string"/>
            <xs:element name="dateTime" type="xs:string"/>
            <xs:element name="userName" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

测试.java

final InputStream commonXsdStream = getXsd(BG_GEN_RESP_XSD_PATH, COMMON);

ClassPathResource fullXsdListing = new ClassPathResource(BG_GEN_RESP_XSD_PATH);

File[] allXsds = fullXsdListing.getFile().listFiles();

for (File currentXsd : allXsds) {
    final int filenameLength = currentXsd.getName().length();
    final String filenameSanExt = currentXsd.getName().substring(0, filenameLength - 4);

    if (!IGNORE.contains(filenameSanExt)) {
        final InputStream xsltStream = getXslt(BG_GEN_RESP_XSLT_PATH, filenameSanExt);
        final InputStream xsdStream = getXsd(BG_GEN_RESP_XSD_PATH, filenameSanExt);

        TransformerFactory xmlTransformer = TransformerFactory.newInstance();
        Templates xsltTemplate = xmlTransformer.newTemplates(new StreamSource(xsltStream));
        final XSLToXMLConvertor converter = new XSLToXMLConvertor();
        String generatedXml = converter.getXML(inputData, xsltTemplate);

        LOG.info(generatedXml);

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(lnew StreamSource(xsdStream));

        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(new StringReader(generatedXml)));

        /*
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(true);
        docBuilderFactory.setValidating(true);

        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        docBuilder.parse(new InputSource(new ByteArrayInputStream(generatedXml.getBytes("utf-8"))));
        */
        }
    }
}
4

1 回答 1

0

定义命名空间和 targetNamespace 通常是个好主意,尽管正如 Petru Gardea 指出的那样,这并不是绝对必要的。这是一个绝对有效的组合:

AccountList.xsd

<xs:schema
    version="1.0"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tns="http://www.myorg.com/xsd/accList"
    targetNamespace="http://www.myorg.com/xsd/accList"
    xmlns:common="http://www.myorg.com/xsd/gen_fin">

    <xs:import namespace="http://www.myorg.com/xsd/gen_fin" schemaLocation="common.xsd" />

    <xs:element name="fundamo">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="response" type="common:response"
                    minOccurs="1" maxOccurs="1" />
                <xs:element name="transaction" type="tns:transaction"
                    minOccurs="0" maxOccurs="1" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="transaction">
        <xs:sequence>
            <xs:element name="transactionRef" type="xs:string" />
            <xs:element name="dateTime" type="xs:string" />
            <xs:element name="userName" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

常见的.xsd

<xs:schema
    version="1.0"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    xmlns="http://www.myorg.com/xsd/gen_fin"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.myorg.com/xsd/gen_fin">

    <xs:complexType name="response">
        <xs:sequence>
            <xs:element name="code" type="xs:string" />
            <xs:element name="description" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:schema>

NewFile.xml(基于该架构):

<tns:fundamo xmlns:p="http://www.myorg.com/xsd/gen_fin"
    xmlns:tns="http://www.myorg.com/xsd/accList" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.myorg.com/xsd/accList AccountList.xsd ">
    <tns:response>
        <p:code>p:code</p:code>
        <p:description>p:description</p:description>
    </tns:response>
</tns:fundamo>

ValidateXml.java:

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

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

public class ValidateXml {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilderFactory.setNamespaceAware(true);
            DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
            Document document = parser.parse(new File("NewFile.xml"));

            Schema schema = schemaFactory.newSchema(new File("AccountList.xsd"));
            Validator validator = schema.newValidator();

            validator.validate(new DOMSource(document));
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

与“找不到元素的声明”相关的错误通常与 XML 文档不支持命名空间有关。验证您的两个 XSD 的路径是否正确,然后返回到您构建可识别名称空间的 XML 文档的代码块。

于 2012-07-27T14:17:42.047 回答