【问题标题】:Is it possible to get <value-of select=''xpath here''> values from a schematron assert with libxml2 C API?是否可以使用 libxml2 C API 从 schematron 断言中获取 <value-of select=''xpath here''> 值?
【发布时间】:2016-11-01 10:22:44
【问题描述】:

注意:我是这些技术的初学者。

我制作了一个 a.sch(schematron 文件)和 b.xml,我正在使用 libxml2 C API 进行验证。除了我在断言消息中没有看到&lt;value-of select=''xpath here''&gt; 的值之外,一切都运行良好。我正在使用可在 libxml2 git 存储库 (https://git.gnome.org/browse/libxml2) 中找到的 xmllint 应用程序。命令行和结果显示在本文末尾。

如果我使用相同的文件并使用以下 python 代码,我可以看到断言消息中的值。

#!/usr/bin/env python

from lxml import isoschematron
from lxml import etree

def runsch(rulesFile, xmlFile):

    # open files
    rules = open(rulesFile, 'r') # Schematron schema
    XMLhere = open (xmlFile, 'r') # XML to be checked

    # Parse schema
    sct_doc = etree.parse(rules)
    schematron = isoschematron.Schematron(sct_doc, store_report = True)

    # Parse xml
    doc = etree.parse(XMLhere)

    # Validate against schema
    validationResult = schematron.validate(doc)
    report = schematron.validation_report

    # Check result
    if validationResult:
        print("passed")
    else:
        print("failed")
        print(report)

        reportFile = open('report.html', 'wb')
        report.write(reportFile)

def main():
    runsch('a.sch', 'b.xml')

if __name__ == '__main__':
    main()

这是 a.sch:

<?xml version="1.0" encoding="utf-8"?>

<schema xmlns="http://purl.oclc.org/dsdl/schematron" schemaVersion="1.0" xml:lang="en" >
    <title>Different behavior of libxml2 C API vs lxml Python regarding value-of select in assert message</title>
    <ns prefix="test" uri="test"/>

    <pattern id="a">
        <rule context="test:b">
            <assert test="count(test:c) = 3">There's only <value-of select="count(test:c)"/> c element(s), it is mandatory to have 3.</assert>
        </rule>
    </pattern>
</schema>

这是 b.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<?valbuddy_schematron a.sch?>
<a xmlns="test" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <b>
        <c>word 1</c>
        <c>word 2</c>
    </b>
</a>

这是使用 libxml2 C API 的命令行(将文件路径替换为您的路径):

xmllint --schematron ~/svn/r-oltcms/Config/a.sch ~/svn/r-oltcms/Config/b.xml

这是部分结果,我们可以看到在断言​​消息中我们没有得到 select 的 value-of。

> /*/* line 4: There's only  c element(s), it is mandatory to have 3.
> /home/oltcms/svn/r-oltcms/Config/b.xml fails to validate

这是python脚本的一部分结果,我们可以看到我们在assert消息中得到了select的value-of。

> <svrl:failed-assert test="count(test:c) = 3"
> location="/*[local-name()='a' and
> namespace-uri()='test']/*[local-name()='b' and
> namespace-uri()='test']">
>     <svrl:text>There's only 2 c element(s), it is mandatory to have 3.</svrl:text> </svrl:failed-assert>

那么,有没有办法使用 libxml2 C API 在断言消息中获取这些信息?欢迎任何解释。

谢谢,

米歇尔

【问题讨论】:

    标签: c xml xpath libxml2 schematron


    【解决方案1】:

    我认为libxml2 只实现了an early Schematron version,可能是1.5 或1.6 不支持value-of 中的value-oflibxml2 的 Schematron 验证器的 Python 绑定位于 lxml.etree.Schematron。但是lxml 也提供了lxml.isoschematron.Schematron,它似乎支持更新的Schematron 版本。此验证器不是直接基于 libxml2,而是基于 XSLT 转换。因此,可能无法使用 libxml2 的 C API 或 xmllintassert 中的 value-of 获取值,但您可以尝试将 value-of 放入 @987654331 @。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2021-05-25
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      相关资源
      最近更新 更多