是否可以使用 libxml2 C API 从 schematron 断言中获取<此处的 select=xpath> 值?



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

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

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

#!/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

下面是结果的一部分,我们可以看到,在assert消息中我们没有得到select的值。

> /*/* 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的值。

> <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?欢迎任何解释。

谢谢,米歇尔

我认为libxml2只实现了早期的Schematron版本,可能是1.5或1.6,在assert中不支持value-of。Python绑定到libxml2的Schematron验证器在lxml.etree.Schematron中。但是lxml也提供了lxml.isoschematron.Schematron,它似乎支持一个更新的Schematron版本。这个验证器不是直接基于libxml2,而是基于XSLT转换。因此,可能没有办法从value-ofassert使用libxml2的C API或xmllint的值,但你可以尝试把value-ofdiagnostic

最新更新