将两个 xsl 文件的功能合并到一个文件中(不是 xsl 导入或包含问题) [英] Merge functionality of two xsl files into a single file (not a xsl import or include issue)

查看:20
本文介绍了将两个 xsl 文件的功能合并到一个文件中(不是 xsl 导入或包含问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two xsl files; both of them perform different tasks on source xml one after another. Now I need a single xsl file which will actually perform both these tasks in single file (its not an issue of xsl import or xsl include):

说我的源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) 删除所有值为空或空的节点:

<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 (tr.xsl) 而不是这两个 xsl(tr1.xsl 和 tr2.xsl),它给我最终输出?

My concern is that instead of these two xsl (tr1.xsl and tr2.xsl) I only need a single xsl (tr.xsl) which gives me final output?

说当我将这两者结合起来

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

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

Only replacement is performed but not null/blank node removal.

推荐答案

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

Applied to this well-formed 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>

这篇关于将两个 xsl 文件的功能合并到一个文件中(不是 xsl 导入或包含问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆