【问题标题】:schematron validation with python and xslt 2.0使用 python 和 xslt 2.0 进行 schematron 验证
【发布时间】:2020-12-03 21:21:28
【问题描述】:

我有一个使用 XSLT 2.0 的 Schematron 文档,我正在寻找一种在 python 中使用 Schematron 规则验证一系列 xml 的方法。

我尝试了 lxml,但它不支持 XSLT 2.0,我还尝试使用 saxonc api,当我尝试初始化它时,它似乎只是崩溃了。

有没有人成功地在 python 中处理 XSLT 2.0 以进行 Schematron 验证?

【问题讨论】:

  • 您在哪个平台上使用 Saxon-C 和 Python?它是否会在您尝试使用它时崩溃?或者只是用您的 Schematron 编译的 XSLT?您使用的是哪种 Schematron 实现(Skeleton 或 Schxslt)?

标签: python-3.x xslt-2.0 schematron


【解决方案1】:

我从 https://github.com/schxslt/schxslt/releases/tag/v1.5.2 下载了最新的 Schxslt 版本 1.5.2 schxslt-1.5.2-xslt-only.zip,并在 Windows 上使用 Python 3.7 和 Saxon-C HE 1.2.1 运行以下示例 Python 程序:

import saxonc

with saxonc.PySaxonProcessor(license=False) as proc:
    print("Test Saxon/C on Python")
    print(proc.version)

    xslt30_processor = proc.new_xslt30_processor()

    xslt30_processor.set_cwd(".")

    xslt30_processor.transform_to_file(source_file="price-xslt2.sch", stylesheet_file="../../../schxslt-1.5.2/2.0/pipeline-for-svrl.xsl", output_file="price-compiled-saxon-c.xsl")

    xslt30_processor.transform_to_file(source_file="books.xml", stylesheet_file="price-compiled-saxon-c.xsl", output_file="saxon-c-report-books.xml")

运行良好,第一个 transform_to_file 调用会生成一个 XSLT 文件 price-compiled-saxon-c.xsl,第二个 transform_to_file 调用适用于输入样本并生成一个验证报告 saxon-c-report-books.xml

如果您想避免中间文件,那么以下方法也可以:

import saxonc

with saxonc.PySaxonProcessor(license=False) as proc:
    print("Test Saxon/C on Python")
    print(proc.version)

    xslt30_processor = proc.new_xslt30_processor()

    xslt30_processor.set_cwd(".")

    compiled_schematron = xslt30_processor.transform_to_value(source_file="price-xslt2.sch", stylesheet_file="../../../schxslt-1.5.2/2.0/pipeline-for-svrl.xsl")
    
    stylesheet_node = compiled_schematron.item_at(0).get_node_value()

    xslt30_processor.compile_stylesheet(stylesheet_node = stylesheet_node)

    xslt30_processor.transform_to_file(source_file="books.xml", output_file="saxon-c-report-3-books.xml")

唯一未知的因素是我安装的 Saxon-C 1.2.1,我不知道它是否与您可以从 Saxonica 下载的最新版本相同,因为它已经有一年的历史了,并且在 https://saxonica.plan.io/projects/saxon-c 上有各种错误报告导致了一些我可能已经应用的修复;不幸的是,到目前为止,Saxonica 从未发布包含所有修复的新维护版本。

如果您在从 Python 运行 Saxon-C 时遇到问题,我想最好在他们的支持论坛上打开一个问题或问题,并提供所有最少但完整的细节来重现它,我相信他们会帮助您告诉你如何启动和运行。

【讨论】:

    猜你喜欢
    • 2011-03-10
    • 2011-06-05
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多