带有实体引用的 Saxon php xml 架构验证

时间:2021-06-11 10:47:28

标签: php xml xsd saxon saxon-c

我正在使用 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 一起提供的示例,但可以确定解决方案..

我如何提及在何处查找实体文件的 Schema 验证器。 并且也可以一次得到所有错误,因为在这种情况下,验证只返回一个 &nbsp; 问题,因为文件中有两个 &nbsp;

1 个答案:

答案 0 :(得分:2)

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

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