Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 将两个xsl文件的功能合并到一个文件中(不是xsl导入或包含问题)_Xslt - Fatal编程技术网

Xslt 将两个xsl文件的功能合并到一个文件中(不是xsl导入或包含问题)

Xslt 将两个xsl文件的功能合并到一个文件中(不是xsl导入或包含问题),xslt,Xslt,我有两个xsl文件;它们都在源xml上一个接一个地执行不同的任务。现在我需要一个单独的xsl文件,它将在单个文件中实际执行这两个任务,这不是xsl导入或xsl的问题,包括: 假设我的源xml是: <LIST_R7P1_1> <R7P1_1> <LVL2> <ORIG_EXP_PRE_CONV>#+#</ORIG_EXP_PRE_CONV> <EXP_AFT_C

我有两个xsl文件;它们都在源xml上一个接一个地执行不同的任务。现在我需要一个单独的xsl文件,它将在单个文件中实际执行这两个任务,这不是xsl导入或xsl的问题,包括:

假设我的源xml是:

<LIST_R7P1_1>
    <R7P1_1>
        <LVL2>
            <ORIG_EXP_PRE_CONV>#+#</ORIG_EXP_PRE_CONV>
            <EXP_AFT_CONV>abc</EXP_AFT_CONV>
            <GUARANTEE_AMOUNT>#+#</GUARANTEE_AMOUNT>
            <CREDIT_DER/>
        </LVL2>
        <LVL21>
            <AZ>#+#</AZ>
            <BZ>bz1</BZ>
            <AZ>az2</AZ>
            <BZ>#+#</BZ>
            <CZ/>
        </LVL21>
    </R7P1_1>
</LIST_R7P1_1>
我的第一个xsl tr1.xsl将删除值为空或null的所有节点:

这里的输出是

我的第二个xsl tr2.xsl在第一个xsl的输出中将+替换为文本空白:

所以我的最终输出是

我担心的是,我不需要这两个xsl tr1.xsl和tr2.xsl,而只需要一个xsl tr.xsl,它将为我提供最终输出

当我将这两个组合为

它输出:

仅执行替换,但不删除空/空节点。

此XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()">
    <xsl:call-template name="globalReplace">
        <xsl:with-param name="outputString" select="."/>
        <xsl:with-param name="target" select="'#+#'"/>
        <xsl:with-param name="replacement" select="''"/>
    </xsl:call-template>
</xsl:template>    

<xsl:template match="*[not(text()) and not(*) and not(@*)]"/>

<xsl:template name="globalReplace">
    <xsl:param name="outputString"/>
    <xsl:param name="target"/>
    <xsl:param name="replacement"/>
    <xsl:choose>
        <xsl:when test="contains($outputString,$target)">
            <xsl:value-of
                    select="concat(
                    substring-before($outputString,$target)
                    ,$replacement)"/>
            <xsl:call-template name="globalReplace">
                <xsl:with-param name="outputString"
                                select="substring-after($outputString,$target)"/>
                <xsl:with-param name="target" select="$target"/>
                <xsl:with-param name="replacement" select="$replacement"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$outputString"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>
应用于此格式良好的XML:

<?xml version="1.0" encoding="UTF-8"?>
<LIST_R7P1_1>
  <R7P1_1>
    <LVL2>
        <ORIG_EXP_PRE_CONV>#+#</ORIG_EXP_PRE_CONV>
        <EXP_AFT_CONV>abc</EXP_AFT_CONV>
        <GUARANTEE_AMOUNT>#+#</GUARANTEE_AMOUNT>
        <CREDIT_DER/>
    </LVL2>
    <LVL21>
        <AZ>#+#</AZ>
        <BZ>bz1</BZ>
        <AZ>az2</AZ>
        <BZ>#+#</BZ>
        <CZ/>
        <ONE_MORE_TEST>#+#OLOLO</ONE_MORE_TEST>
    </LVL21>
  </R7P1_1>
</LIST_R7P1_1>
产生以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<LIST_R7P1_1>
  <R7P1_1>
    <LVL2>
        <ORIG_EXP_PRE_CONV/>
        <EXP_AFT_CONV>abc</EXP_AFT_CONV>
        <GUARANTEE_AMOUNT/>
    </LVL2>
    <LVL21>
        <AZ/>
        <BZ>bz1</BZ>
        <AZ>az2</AZ>
        <BZ/>
        <ONE_MORE_TEST>OLOLO</ONE_MORE_TEST>
    </LVL21>
  </R7P1_1>
</LIST_R7P1_1>
此XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="text()">
    <xsl:call-template name="globalReplace">
        <xsl:with-param name="outputString" select="."/>
        <xsl:with-param name="target" select="'#+#'"/>
        <xsl:with-param name="replacement" select="''"/>
    </xsl:call-template>
</xsl:template>    

<xsl:template match="*[not(text()) and not(*) and not(@*)]"/>

<xsl:template name="globalReplace">
    <xsl:param name="outputString"/>
    <xsl:param name="target"/>
    <xsl:param name="replacement"/>
    <xsl:choose>
        <xsl:when test="contains($outputString,$target)">
            <xsl:value-of
                    select="concat(
                    substring-before($outputString,$target)
                    ,$replacement)"/>
            <xsl:call-template name="globalReplace">
                <xsl:with-param name="outputString"
                                select="substring-after($outputString,$target)"/>
                <xsl:with-param name="target" select="$target"/>
                <xsl:with-param name="replacement" select="$replacement"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$outputString"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>
应用于此格式良好的XML:

<?xml version="1.0" encoding="UTF-8"?>
<LIST_R7P1_1>
  <R7P1_1>
    <LVL2>
        <ORIG_EXP_PRE_CONV>#+#</ORIG_EXP_PRE_CONV>
        <EXP_AFT_CONV>abc</EXP_AFT_CONV>
        <GUARANTEE_AMOUNT>#+#</GUARANTEE_AMOUNT>
        <CREDIT_DER/>
    </LVL2>
    <LVL21>
        <AZ>#+#</AZ>
        <BZ>bz1</BZ>
        <AZ>az2</AZ>
        <BZ>#+#</BZ>
        <CZ/>
        <ONE_MORE_TEST>#+#OLOLO</ONE_MORE_TEST>
    </LVL21>
  </R7P1_1>
</LIST_R7P1_1>
产生以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<LIST_R7P1_1>
  <R7P1_1>
    <LVL2>
        <ORIG_EXP_PRE_CONV/>
        <EXP_AFT_CONV>abc</EXP_AFT_CONV>
        <GUARANTEE_AMOUNT/>
    </LVL2>
    <LVL21>
        <AZ/>
        <BZ>bz1</BZ>
        <AZ>az2</AZ>
        <BZ/>
        <ONE_MORE_TEST>OLOLO</ONE_MORE_TEST>
    </LVL21>
  </R7P1_1>
</LIST_R7P1_1>

该问题的一般解决方案是更改模板规则,并在一个样式表中应用模板调用以使用模式M1,在另一个样式表中应用模板调用以使用模式M2,然后按如下方式组合它们:

<xsl:template match="/">
  <xsl:variable name="temp">
    <xsl:apply-templates select="." mode="M1"/>
  </xsl:variable>
  <xsl:apply-templates select="$temp" mode="M2"/>
</xsl:template>
但是在XSLT1.0中,第二个应用模板需要

<xsl:apply-templates select="exslt:node-set($temp)" mode="M2"/>

该问题的一般解决方案是更改模板规则,并在一个样式表中应用模板调用以使用模式M1,在另一个样式表中应用模板调用以使用模式M2,然后按如下方式组合它们:

<xsl:template match="/">
  <xsl:variable name="temp">
    <xsl:apply-templates select="." mode="M1"/>
  </xsl:variable>
  <xsl:apply-templates select="$temp" mode="M2"/>
</xsl:template>
但是在XSLT1.0中,第二个应用模板需要

<xsl:apply-templates select="exslt:node-set($temp)" mode="M2"/>

感谢回复,输出不应该删除包含value+的节点,而应该将其设置为null节点,因为+应该变成;只有值为null的节点才应该从xml中完全删除。所以最终的输出应该是:abcbz1az2刚刚删除或text='+'。我将在一秒钟内编辑我的答案。此时,我们通过接受并向上投票我答案左上角的答案复选标记来表达我们的感激之情。感谢回复,输出不应删除包含值+的节点,而应将其设为空节点,因为+应变为空节点;只有值为null的节点才应该从xml中完全删除。所以最终的输出应该是:abcbz1az2刚刚删除或text='+'。我将在一秒钟内编辑我的答案。此时,我们接受并向上投票我答案左上角的答案复选标记,以表达我们的感激之情。嗨,Flack,同样的问题需要再次帮助,请在问题末尾再次检查我的问题,我添加了进一步的问题。Flack,请查看我的新问题,不要重新发布问题,不要混合使用标记,使用或{}编辑按钮我需要对同一问题再次提供帮助,请在问题末尾再次检查我的问题,我添加了进一步的问题。Flack请查看我的新问题,请不要重新发布问题,不要混合使用标记,使用或{}编辑按钮如果OP很好地说明了它们可以手动合并,那么为什么要对它们进行管道化处理?@Flack:管道化比手动合并好——前者可以自动进行,而后者则必须再次经历开发的所有阶段。与手动合并相比,流水线只需要很少的工作量。流水线的另一大优势是它允许代码重用。但是,如果OP很好地(不是很明显地)说明它们可以手动合并,为什么还要对它们进行流水线呢?@Flack:流水线比手动合并好——前者可以自动进行,而后者则必须再次经历开发的所有阶段。与手动合并相比,流水线只需要很少的工作量。流水线的另一大优势是它允许代码重用。
<xsl:apply-templates select="exslt:node-set($temp)" mode="M2"/>