[英]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.