XSLT 3.0 模板规则不可流化

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

我正在使用下面的 xsl 创建 csv 文件。我需要处理条件检查,如果 XML 不包含项目元素,那么我需要添加默认行,否则需要添加项目中的元素。

我的 XML 很大,并且我正在使用 SAXON 流。当我同时使用 if 和 for-each 元素时,它会抛出错误。我需要你的帮助来处理这个问题。

发生错误:转换时发生错误:模板规则不可流化

  • 有多个消耗操作数:
  • 模板规则的结果可以包含流式节点

XSLT

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:output method="text" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>
    <xsl:mode streamable="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:variable name="dq" select="'&quot;'"/>
    <xsl:variable name="qcq" select="'&quot;,&quot;'"/>
    <xsl:variable name="lf" select="'&#10;'"/>   

    <xsl:variable name="email" select="'[email protected]'"/>
    <xsl:variable name="product_id" select="'product1'"/>
    <xsl:template match="items">
        <xsl:text>product_id,email,key</xsl:text>
        <xsl:value-of select="'&#10;'"/>
        <xsl:choose>
            <xsl:when test="not(item)">
                <xsl:value-of disable-output-escaping="yes" select="concat($dq,$qcq,$email,$qcq,$dq,$lf)"/>
            </xsl:when>
            <xsl:when test="item">
                <xsl:for-each select="item ! copy-of(.)">
                    <xsl:variable name="p_key" select="id"/>
                    <xsl:value-of disable-output-escaping="yes" select="concat($dq,$product_id,$qcq,$email,$qcq,$p_key,$dq,$lf)"/>
                </xsl:for-each>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet> 

Sample XML


<?xml version="1.0" encoding="UTF-8"?>
<items><item><id><![CDATA[040411c47702b29acb4e4120040ee6b937fbb8]]></id></item></items>

no item in xml
<?xml version="1.0" encoding="UTF-8"?>
<items></items>
xslt saxon
1个回答
0
投票

如果

items
元素可以为空,您可以使用例如

   <xsl:template match="items">
        <xsl:text>product_id,email,key</xsl:text>
        <xsl:value-of select="'&#10;'"/>
        <xsl:choose>
            <xsl:when test="not(has-children())">
                <xsl:value-of select="concat($dq,$qcq,$email,$qcq,$dq,$lf)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:for-each select="item ! copy-of(.)">
                    <xsl:variable name="p_key" select="id"/>
                    <xsl:value-of select="concat($dq,$product_id,$qcq,$email,$qcq,$p_key,$dq,$lf)"/>
                </xsl:for-each>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

我已经删除了禁用输出转义,因为在我看来它对于输出方法文本没有意义。

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