首页 > 解决方案 > 带有实体引用的 Saxon php xml 模式验证

问题描述

我正在使用 saxon c api EE 版本开发一个 php 应用程序,它需要针对 xsd 模式验证 xml 文件。

我在进行验证时收到以下错误。

org.xml.sax.SAXParseException; systemId: file:**path**/temp.xml; lineNumber: 6; columnNumber: 48; The entity "nbsp" was referenced, but not declared

我的 xml 文件内容是

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE section [
<!ENTITY % ent1 SYSTEM "isonum.ent">
]>
<section>
    <section-heading>This is a test Heading &nbsp; and &amp; check</section-heading>
    <section>
        <section-heading>Another sub section heading with &nbsp; and &amp; check</section-heading>
        
    </section>
</section>

xml 中有一个实体文件的引用,该文件isonum.ent位于 xml 文件所在的同一路径中。

实体文件有定义  

<!ENTITY rdquo  "&#x201D;" ><!--=double quotation mark, right-->
<!ENTITY nbsp   "&#160;" ><!--=no break (required) space-->
<!ENTITY shy    "&#173;" ><!--=soft hyphen-->

我用于验证的 php 代码如下

    $proc = new Saxon\SaxonProcessor(true);
    $proc->setConfigurationProperty("xsdversion", "1.1");
    $proc->setConfigurationProperty("http://saxon.sf.net/feature/validationWarnings", "true");
    $proc->setConfigurationProperty("http://saxon.sf.net/feature/multipleSchemaImports", "on");

    $val = $proc->newSchemaValidator();
    $val->registerSchemaFromFile($xsd_path);
    $val->setProperty("report-node", "true");    
    $val->setProperty("verbose", "true");
    $val->validate($xml_path);

我参考了https://www.saxonica.com/saxon-c/documentation/index.html中提供的文档以及库下载 zip 提供的示例,但可以确定解决方案。

我怎么能提到架构验证器在哪里寻找实体文件。并且也可以一次得到所有错误,因为在这种情况下,验证只返回一个&nbsp;问题,因为文件中有两个&nbsp;'s。

标签: phpxmlxsdsaxonsaxon-c

解决方案


结果证明这是一个简单的用户错误。DTD 声明了一个参数实体,但没有引用它,因此参数实体的内容不会成为 DTD 的一部分。它需要写成:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE section [
<!ENTITY % ent1 SYSTEM "isonum.ent">
%ent1;
]>

推荐阅读