在尝试从Python代码向XSLT文件传递参数时获取“xsltCompilePattern”

2024-10-03 11:14:44 发布

您现在位置:Python中文网/ 问答频道 /正文

transform.xslt:

<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="no"/>
    <xsl:param name="tag"/>
    <xsl:param name="uniqueID"/>
    <xsl:param name="uniqueIDValue"/>
    <xsl:param name="attributeToBeAdded"/>
    <xsl:param name="attributeValue"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="{ $tag }[@{ $uniqueID }={ $uniqueIDValue }]">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:attribute name="{$attributeToBeAdded}">{$attributeValue}</xsl:attribute>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>
</xsl:transform>

Python代码:

xml = ET.parse('ABC.xml')
xslt = ET.parse('transform.xslt')
transform = ET.XSLT(xslt)
newdom = xslt(xml, tag=ET.XSLT.strparam("wire"),uniqueID=ET.XSLT.strparam("name"),uniqueIDValue=ET.XSLT.strparam("ABC"),attributeToBeAdded=ET.XSLT.strparam("Fr"),attributeValue=ET.XSLT.strparam("351"))

在运行python代码向xslt文件传递参数时,我遇到以下错误

xsltparserror:xsltCompilePattern:未能编译“{$tag}[@{$uniqueID}={$uniqueIDValue}]”

我很确定我犯了一些语法错误,需要正确语法的帮助。谢谢


Tags: nameparamtagtransformtemplatexmletcopy
1条回答
网友
1楼 · 发布于 2024-10-03 11:14:44

不清楚您期望得到什么,通常的Python XSLT支持是基于libxslt的,libxslt支持XSLT1,因此我不理解您为什么在XSLT版本2.0和3.0中使用这些标记。此外,在任何版本的XSLT中,无论您以何种方式设置参数,都不支持您尝试的match方式,最接近的方式是将XSLT 3处理器与静态参数和阴影属性一起使用,例如_match。这需要使用XSLT3处理器,如Saxon-C1.2.1及其Python绑定,尽管我目前不确定该设置中静态参数的支持程度

文本值模板语法{$attributeValue}也仅由XSLT 3处理器支持,甚至只有在XSLT中设置expand-text="yes"时才支持

因此,就XSLT1而言,唯一正确的用法是xsl:attribute name="{$attributeToBeAdded}"

相关问题 更多 >