【发布时间】:2016-11-01 10:22:44
【问题描述】:
注意:我是这些技术的初学者。
我制作了一个 a.sch(schematron 文件)和 b.xml,我正在使用 libxml2 C API 进行验证。除了我在断言消息中没有看到<value-of select=''xpath here''> 的值之外,一切都运行良好。我正在使用可在 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