简体   繁体   English

XSLT 使用 xdm_node 的转换返回错误 - 将 Saxon-HE 9.9.1.5C 与 Python 3.9 结合使用

[英]XSLT transformation using a xdm_node returns an error - Using Saxon-HE 9.9.1.5C with Python 3.9

I have a problem with Saxon HE in Python. When I parse a XML file I get as return a type PyXdmNode.我在 Python 中遇到 Saxon HE 问题。当我解析 XML 文件时,我得到一个 PyXdmNode 类型的返回值。

After that I want to do an XSLT transformation and use the method transform_to_value(xdm_node=... ).之后我想进行 XSLT 转换并使用方法 transform_to_value(xdm_node=... )。

When I do this, I get the following error:当我这样做时,出现以下错误:

... File "saxonc.pyx", line 781, in saxonc.PyXsltProcessor.transform_to_value self.setSourceFromXdmNode(value) AttributeError: 'saxonc.PyXsltProcessor' object has no attribute 'setSourceFromXdmNode'... ...文件“saxonc.pyx”,第 781 行,在 saxonc.PyXsltProcessor.transform_to_value self.setSourceFromXdmNode(value) AttributeError: 'saxonc.PyXsltProcessor' object 中没有属性 'setSourceFromXdmNode'...

What am I doing wrong?我究竟做错了什么? It almost looks like in Python an XSLT transformation only works with transform_to_value(source_file = '...' ).它几乎看起来像在 Python 中,XSLT 转换仅适用于 transform_to_value(source_file = '...' )。

Python File: Python 文件:

import saxonc

proc = saxonc.PySaxonProcessor(license=False)
print(f"\n{proc.version}")
xml = proc.parse_xml(xml_file_name="Test_xml.xml")
# <class 'saxonc.PyXdmNode'>
print(type(xml))

xslt_proc = proc.new_xslt_processor()
xslt_proc.compile_stylesheet(stylesheet_file="Test_xslt.xslt")

# Error line
xml_trans_1 = xslt_proc.transform_to_value(xdm_node= xml)

# All the same only different **kwargs - Works fine
xml_trans_2 = xslt_proc.transform_to_value(source_file= "Test_xml.xml")

XML File - Test_xml.xml: XML 文件 - Test_xml.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<TEST Id="T-1">
    <FOO/>
</TEST>

XSLT File - Test_xslt.xslt: XSLT 文件 - Test_xslt.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"   xmlns:xsl= "http://www.w3.org/1999/XSL/Transform">
    
    <xsl:output method="xml" indent="yes" />
    
    
    <xsl:template match="@*|node()"> 
        <xsl:copy> 
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="FOO"> 
        <xsl:element name="BAR">
            <xsl:attribute name="Id">
                <xsl:value-of select="'Hello World'"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet> 

Result:结果:

<TEST Id="T-1">
   <BAR Id="Hello World"/>
</TEST>

With SaxonC 11.3, you can use apply_templates_returning_value(xdm_value = xml) eg使用 SaxonC 11.3,您可以使用apply_templates_returning_value(xdm_value = xml)例如

from saxonc import *

with PySaxonProcessor(license=False) as processor:
    print("Test SaxonC on Python")
    print(processor.version)

    xml_doc = processor.parse_xml(xml_file_name = 'sample1.xml')

    xslt30_processor = processor.new_xslt30_processor()

    xslt30_transformer = xslt30_processor.compile_stylesheet(stylesheet_file = 'sheet1.xsl')

    result = xslt30_transformer.apply_templates_returning_value(xdm_value = xml_doc)

    print(result)

Some other API methods have been changed/fixed, see https://saxonica.plan.io/issues/5446 , but I think we have to wait for 11.4 to use the fix.其他一些 API 方法已更改/修复,请参阅https://saxonica.plan.io/issues/5446 ,但我认为我们必须等待 11.4 才能使用该修复程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Saxon C HE 和 Python 时没有 DTD 验证和 XInclude 解析 - No DTD validation and XInclude resolution when using Saxon C HE with Python saxonc 的 transform_to_file() 在提供 xdm_node 时不产生 output 文件 - saxonc's transform_to_file() produces no output file when supplying xdm_node 使用 nvm 安装节点 14.18.1 时,我的 mac 使用 python 3.9 并在安装时抛出错误 - When installing node 14.18.1 using nvm my mac uses python 3.9 and throws error on installation CUDA 错误:使用 python 3.9 时设备序号无效 - CUDA error: invalid device ordinal when using python 3.9 将 Python 3.9 与 .networkx package 一起使用,出现错误:KeyError - Using Python 3.9 with the networkx package and got the error: KeyError 在 Saxon/C 中使用条件包含/静态参数? - Using conditional includes/static parameters in Saxon/C? 使用 ZA7F5F35426B927411FC9231B563821 中的 Pandas 将 Excel 转换为 JSON。 - Converting Excel to JSON using Pandas in Python 3.9 无法使用 conda 安装 python 3.9 - Cannot install python 3.9 using conda 如何在 python 3.9 中使用 %APPDATA% 获取文件? - How to get a file using %APPDATA% in python 3.9? 如何使用python lxml加速使用xslt转换大型xml文件 - How to speed up the transformation of large xml files with xslt using python lxml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM