SaxonC-HE 12.4.2传递数组给PyXslt30Processor报AttributeError错误
问题解决:PySaxon传递数组/序列参数到XSLT报错
错误原因
你遇到的AttributeError根源在于:
- 你的XSLT 2.0样式表中,参数
$values默认是**序列(sequence)**类型((2,3,4,5)是序列写法); - Python代码里用
proc.make_array()创建了**XDM数组(array)**对象,两者类型不兼容; - SaxonC 12.4.2版本的
PyXdmArray在set_parameter方法中存在属性缺失问题,导致无法正确传递参数。
解决方案
方案1:传递XDM序列(适配XSLT 2.0)
修改Python代码中创建参数值的逻辑,用make_sequence()替代make_array(),生成与XSLT参数匹配的序列类型:
with PySaxonProcessor(license=False) as proc: print(proc.version) xsltproc = proc.new_xslt30_processor() xsltproc.set_cwd(os.getcwd()) executable = xsltproc.compile_stylesheet(stylesheet_file="cat.xsl") # 替换make_array为make_sequence,生成XDM序列 value = proc.make_sequence([proc.make_integer_value(i) for i in [8,9,10]]) print(value) executable.set_parameter("values", value) result = executable.apply_templates_returning_string(source_file="person.xml") print(result)
方案2:升级XSLT到3.0并使用数组类型(可选)
如果想使用XDM数组特性,可将XSLT升级到3.0版本,显式声明参数为数组类型:
修改XSLT样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="3.0"> <!-- 声明参数为整数数组类型 --> <xsl:param name="values" as="array(xs:integer)" select="[2, 3, 4, 5]"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="*"> <output> <!-- 遍历数组需用array-values()函数 --> <xsl:for-each select="array-values($values)"> <out> <xsl:value-of select=". * 3"/> </out> </xsl:for-each> </output> </xsl:template> </xsl:stylesheet>
Python代码可保留make_array(),但如果仍报错,建议降级到SaxonC 12.3或升级到更高版本,修复PyXdmArray的属性缺失问题。
预期输出
修改后执行代码,会生成符合预期的XML:
<output> <out>24</out> <out>27</out> <out>30</out> </output>
内容的提问来源于stack exchange,提问作者Menzo Windhouwer
相关产品推荐
相关产品推荐

