xslt 如何将两个 XSLT 合并为一个



我有以下XML。第一个XSLT删除重复记录。第二个XSLT将记录格式化为特定的文件格式。如何将第一个和第二个XSLT合并到一个XSLT中以提供所需的结果?

<XML>
    <Record>
        <GroupId>10028</GroupId>
        <SessionId>264-10028-1-515530-2</SessionId>
        <TxnId>264-10028-1-515539-1</TxnId>
        <Date>31-Jul-2014</Date>
        <Time>11:22:40</Time>
        <Account>1111111111</Account>
        <NAD>5000</NAD>
    </Record>
    <Record>
        <GroupId>10028</GroupId>
        <SessionId>264-10028-1-515530-2</SessionId>
        <TxnId>264-10028-1-515539-2</TxnId>
        <Date>31-Jul-2014</Date>
        <Time>11:22:40</Time>
        <Account>2222222222</Account>
        <NAD>6000</NAD>
    </Record>
    <Record>
        <GroupId>10028</GroupId>
        <SessionId>264-10028-1-515545-1</SessionId>
        <TxnId>264-10028-1-515545-2</TxnId>
        <Date>31-Jul-2014</Date>
        <Time>11:22:55</Time>
        <Account>3333333333</Account>
        <NAD>1000</NAD>
    </Record>
    <Record>
        <GroupId>10028</GroupId>
        <TxnId>264-10028-1-515550-1</TxnId>
        <Date>31-Jul-2014</Date>
        <Time>11:23:32</Time>
        <OrigTxnId>264-10028-1-515545-2</OrigTxnId>
        <Account>3333333333</Account>
        <NAD>1000</NAD>
    </Record>
</XML>
XSLT

第一

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="original" match="Record" use="TxnId" />
    <xsl:key name="copy" match="Record" use="OrigTxnId" />
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Record[key('original', OrigTxnId) or key('copy', TxnId)]"/>
</xsl:stylesheet>

第二个XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="no" encoding="iso-8859-1" omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:for-each select="XML/Record">
            <?Record Type - 1 - Fixed value “D”(etail)?>
            <xsl:text>D</xsl:text>
            <?SeqNo - 6 - Right justified, zero padded?>
            <xsl:value-of select="format-number(count(preceding-sibling::Record)+1, '000000')"/>
            <?CompanyCode - 3 - Leave Blank, Space padded?>
            <xsl:text>&#160;&#160;&#160;</xsl:text>
            <?CustAccountNo - 20 - Right justified, zero padded?>
            <xsl:value-of select="format-number(Account, '00000000000000000000')"/>
            <?NamPostBranch - 50 - ?>
            <xsl:call-template name="reformat-string-length">
                <xsl:with-param name="value" select="GroupId"/>
                <xsl:with-param name="str-len" select="50"/>
            </xsl:call-template>
            <?NamPostReceiptNo - 16 - Group-Node-Sequence No?>
            <xsl:call-template name="reformat-string-length">
                <xsl:with-param name="value" select="substring(TxnId,5,16)"/>
                <xsl:with-param name="str-len" select="16"/>
            </xsl:call-template>                            
            <?MOPCheck?>
            <xsl:choose>
                <xsl:when test="ChequeNo &gt; 0">
                    <xsl:text>1</xsl:text>
                    <?BankBranchCode - 6 - space padded?>
                    <xsl:call-template name="reformat-string-length">
                        <xsl:with-param name="value" select="BankBranchCode"/>
                        <xsl:with-param name="str-len" select="6"/>
                    </xsl:call-template>
                    <?ChequeAccountNo -15- Left justified, space padded?>
                    <xsl:call-template name="reformat-string-length">
                        <xsl:with-param name="value" select="ChequeAccNo"/>
                        <xsl:with-param name="str-len" select="15"/>
                    </xsl:call-template>
                    <?ChequeNo - 6 - Right justified, zero padded?>
                    <xsl:value-of select="format-number(ChequeNo, '000000')"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>2</xsl:text>
                    <?BankBranchCode - 6 - space padded?>
                    <xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
                    <?ChequeAccountNo -15- Left justified, space padded?>
                    <xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
                    <?ChequeNo - 6 - Right justified, zero padded?>
                    <xsl:text>&#160;&#160;&#160;&#160;&#160;&#160;</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
            <?PayAmountCents - 9 - Right justified, zero padded?>
            <xsl:value-of select="format-number(NAD, '000000000')"/>
            <?PaymentDateTime (YYYYMMDDHHMMSS)?>
            <xsl:value-of select="substring(Date,8,4)"/>
            <xsl:call-template name="format-month-3letter-to-number">
                <xsl:with-param name="month-3letter" select="substring(Date,4,3)"/>
            </xsl:call-template>
            <xsl:value-of select="substring(Date,1,2)"/>
            <xsl:value-of select="substring(Time,1,2)"/>
            <xsl:value-of select="substring(Time,4,2)"/>
            <xsl:value-of select="substring(Time,7,2)"/>
            <?AmountSign - 1- ?>
            <xsl:text>C</xsl:text>
            <?PolicyNo - 3 - First Three Character Of Account Number?>
            <xsl:text>&#160;&#160;&#160;</xsl:text>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
    <?Support functions ------------------- ?>
    <?Convert month from text to number?>
    <xsl:template name="format-month-3letter-to-number">
        <xsl:param name="month-3letter"/>
        <xsl:variable name="MonthName" select="'Jan01Feb02Mar03Apr04May05Jun06Jul07Aug08Sep09Oct10Nov11Dec12'"/>
        <xsl:value-of select="substring(concat(substring-after($MonthName,$month-3letter),'00'),1,2)"/>
    </xsl:template>
    <?Pad space?>   
    <xsl:template name="pad-some-space"> 
        <xsl:param name="currentlength"/>
        <xsl:param name="newlength"/>
        <xsl:if test="number($currentlength) &lt; number($newlength)">
            <xsl:text>&#160;</xsl:text>
            <xsl:call-template name="pad-some-space">
                <xsl:with-param name="currentlength" select="number($currentlength)+1"/>
                <xsl:with-param name="newlength" select="$newlength" />
            </xsl:call-template>
        </xsl:if> 
    </xsl:template> 
    <?Evaluate-string-length?>
    <xsl:template name="reformat-string-length">
        <xsl:param name="value"/>
        <xsl:param name="str-len"/>
        <xsl:choose>
            <xsl:when test="string-length($value) &gt; number($str-len)">
                <xsl:value-of select="substring($value,1,$str-len)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$value"/>
                <xsl:call-template name="pad-some-space">
                    <xsl:with-param name="currentlength" select="string-length($value)"/>
                    <xsl:with-param name="newlength" select="number($str-len)"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>                   
    </xsl:template>
</xsl:stylesheet>

预期结果:

D000001 0000000000111111111110028 10028-1-515539-12 00000500020140731112240C D000002 0000000000222222222210028 10028-1-515539-22 00000600020140731112240C

在第二个模板中这样定义键:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="no" encoding="iso-8859-1" omit-xml-declaration="yes"/>
    <xsl:key name="original" match="/XML/Record" use="TxnId" />
    <xsl:key name="copy" match="/XML/Record" use="OrigTxnId" />
    <xsl:template match="/">

使用第二个模板的选择器中的键;但是把条件反过来:

    <xsl:template match="/">
        <xsl:for-each select="XML/Record[not(key('original', OrigTxnId) or key('copy', TxnId))]">

最新更新