【问题标题】:Merge functionality of two xsl files into a single file (not a xsl import or include issue)将两个 xsl 文件的功能合并到一个文件中(不是 xsl 导入或包含问题)
【发布时间】:2011-01-10 07:34:24
【问题描述】:

我有两个 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:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:if test=". != '' or ./@* != ''">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

这里的输出

<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>
        </LVL2>
        <LVL21>
            <AZ>#+#</AZ>
            <BZ>bz1</BZ>
            <AZ>az2</AZ>
            <BZ>#+#</BZ>
        </LVL21>
    </R7P1_1>
</LIST_R7P1_1>

并且我的第二个 xsl (tr2.xsl) 对第一个 xsl 的输出进行了全局替换(#+# 文本空白''):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     version="1.0">
    <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: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="@*|*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

所以我的最终输出是

<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>
        </LVL2>
        <LVL21>
            <AZ></AZ>
            <BZ>bz1</BZ>
            <AZ>az2</AZ>
            <BZ></BZ>
        </LVL21>
    </R7P1_1>
</LIST_R7P1_1>

我担心的是,除了这两个 xsl(tr1.xsl 和 tr2.xsl),我只需要一个 xsl(tr.xsl)来给我最终输出?

说当我将这两者结合为

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:if test=". != '' or ./@* != ''">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>
    <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: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="@*|*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

它输出:

<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>

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

【问题讨论】:

  • Hi Flack 再次需要对同一问题的帮助,请再次检查我的问题(在问题的末尾我添加了进一步的问题)。
  • Flack 请查看我的新问题stackoverflow.com/questions/4646329/…
  • 请不要转发问题,也不要混合标记,使用&lt;CODE&gt;{}编辑按钮

标签: xslt


【解决方案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>

但在 XSLT 1.0 中,第二个应用模板需要是

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

【讨论】:

  • 但是,如果 OP 声明(嗯,不是很明显)它们可以手动合并,为什么要“管道”它们呢?
  • @Flack:流水线比手动合并要好——前者可以自动化,而后者必须再次经历所有开发阶段。与手动合并相比,流水线化的工作量非常小。流水线的另一大优势是它允许代码重用。
【解决方案2】:

这个 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>

【讨论】:

  • 感谢回复,输出不应删除包含值 #+# 的节点,而应使其为空节点,因为 #+# 应变为 ;只有具有空值的节点说 应该从 xml 中完全删除。所以最终输出应该是: abcbz1az2
  • 只需删除 或 text() = '#+#'。我将在几秒钟内编辑我的答案。
  • 在此,在 SO,我们通过接受和支持答案来表达我们的感激之情(我的答案左上角的复选标记)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多