Merge functionality of two xsl files into a single file (not a xsl import or include issue)

后端 未结 2 1080
一个人的身影
一个人的身影 2021-01-24 22:52

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 f

相关标签:
2条回答
  • 2021-01-24 23:24

    The general solution to the problem is to change the template rules and apply-templates calls in one stylesheet to use mode M1, and those in the other to use mode M2, and then to combine them like this:

    <xsl:template match="/">
      <xsl:variable name="temp">
        <xsl:apply-templates select="." mode="M1"/>
      </xsl:variable>
      <xsl:apply-templates select="$temp" mode="M2"/>
    </xsl:template>
    

    But in XSLT 1.0 the second apply-templates will need to be

    <xsl:apply-templates select="exslt:node-set($temp)" mode="M2"/>
    
    0 讨论(0)
  • 2021-01-24 23:31

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

    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>
    

    Produces this result:

    <?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>
    
    0 讨论(0)
提交回复
热议问题