python saxonche - 为什么executable.transform_to_value 返回 None?

问题描述 投票:0回答:1

刚刚找到了 python 的 saxonche 库。这太棒了。在玩的时候我遇到了以下问题:

当尝试使用

executable.transform_to_value
返回 PyXdmNode 时,下面的代码不会生成 None。

输入xml:

<document>
    <a>123456</a>
</document>

xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <root>
      <b>
        <xsl:value-of select="document/a"/>
      </b>
    </root>
  </xsl:template>
</xsl:stylesheet>

python3.8.10:

from saxonche import PySaxonProcessor

input_xml ="examples/input.xml"
stylesheet = "examples/test.xslt"

proc = PySaxonProcessor(license=False)
xsltproc = proc.new_xslt30_processor()

input_xdm = proc.parse_xml(xml_file_name=input_xml)
print(type(input_xdm))
print("--------------")

stylesheet_xdm = proc.parse_xml(xml_file_name=stylesheet)
print(type(stylesheet_xdm))
print("==============")

executable = xsltproc.compile_stylesheet(stylesheet_node=stylesheet_xdm)

# produces None (what I'm doing wrong?)
result_xdm = executable.transform_to_value(xdm_node=input_xdm)
print(type(result_xdm))
print(result_xdm)

print("--------------")

#produces string (as expected)
result_str = executable.transform_to_string(xdm_node=input_xdm)
print(type(result_str))
print(result_str)

输出:

python3 test_xslt_xdm_node.py
<class 'saxonche.PyXdmNode'>                                                       
--------------                                                                     
<class 'saxonche.PyXdmNode'>                                                       
==============                                                                     
<class 'NoneType'>                                                                 
None                                                                               
--------------                                                                     
<class 'str'>                                                                      
<?xml version="1.0" encoding="UTF-8"?><root><b>123456</b></root>

如您所见,我确实尝试转换为字符串,并且它按预期工作。 有人知道我做错了什么吗?

xslt python-3.8 saxon-c
1个回答
0
投票

这似乎是 SaxonC 中的一个错误,我建议你将 Python 代码更改为

result_xdm = executable.apply_templates_returning_value(xdm_value=input_xdm)
print(type(result_xdm))
print(result_xdm)

作为解决方法。

© www.soinside.com 2019 - 2024. All rights reserved.