SchematronParseError:无效的schematron架构(对于ISOSTS架构)

2024-09-27 20:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Schematron验证文档。在

我用schema for ISOSTS standard。在

from lxml import etree
from lxml.isoschematron import Schematron   


def validate(self, filename: str):
    file = open(filename)

    schema_filename = join('/path/to/ISOSTS_validation.sch')
    schema_file = open(schema_filename)

    # fixme it works. But fails with ISOSTS scheme
    # schema_file = StringIO('''\
    #     <schema xmlns="http://purl.oclc.org/dsdl/schematron" >
    #       <pattern id="sum_equals_100_percent">
    #         <title>Sum equals 100%.</title>
    #         <rule context="Total">
    #           <assert test="sum(//Percent)=100">Sum is not 100%.</assert>
    #         </rule>
    #       </pattern>
    #     </schema>
    # ''')

    sct_doc = etree.parse(schema_file)
    schematron = Schematron(sct_doc)       ## <- FAIL !!!

    doc = etree.parse(file)
    result = schematron.validate(doc)

    file.close()
    schema_file.close()

    return result

validate('/path/to/feature_doc.xml')

错误消息:

^{pr2}$

怎么修理?在


Tags: topathfromimportdocschemaopenfilename
2条回答

我不确定它是否有用,但我不认为问题出在你的代码中。我认为问题是lxml不支持XSLT-2。在

您使用的模式需要符合2010 XSLT-2的ISO Schematron[1]。在

在氧气中打开模式并删除querybinding=xslt2属性会产生大量问题。这包括第553行的验证错误(<xsl:param name="num-cols" as="xs:integer"/>):“此元素上不允许属性”。这是lxml向[2]抛出解析错误的行。在

lxml没有实现XSTL-2,并且明确声明它只支持Schematron的“pure-XSLT-1.0skeleton implementation”(信息来自http://lxml.de/validation.html#id2)。在

如果你想让它和lxml一起工作,你可能会倒霉。据我所知,没有一个与XSLT-2兼容的pythonxml解析器(如果有人知道的话,那就太棒了)。在

这有点麻烦,但您可以使用子流程来使用外部工具(也许是crux+libsaxon)执行验证。这可能是唯一的解决办法。在

[1]链接模式的第35行: <schema xmlns="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt2"

[2]lxml.etree.SchematronParseError: invalid schematron schema: <string>:553:0:ERROR:RELAXNGV:RELAXNG_ERR_EXTRACONTENT: Element function has extra content: param

使用xsd架构from archivelxml.etree.XMLSchema解决:

def validate(self, filename: str):
    file = open(filename)

    schema_filename = '/path/to/ISOSTS.xsd'
    schema_file = open(schema_filename)

    sct_doc = etree.parse(schema_file)
    xmlschema = etree.XMLSchema(sct_doc)

    doc = etree.parse(file)
    result = xmlschema.validate(doc)

    file.close()
    schema_file.close()

    return result

相关问题 更多 >

    热门问题