how to copy the data of variable of one foreach to another for each in xslt

后端 未结 2 1139
不知归路
不知归路 2021-01-17 04:04

XML


 
     
        102
        BKTRU         


        
相关标签:
2条回答
  • 2021-01-17 04:33

    XSLT is a functional language -- among other things this means that variables are immutable -- once given a value they cannot be changed.

    The solution to this specific problem:

    Change:

    <xsl:value-of select="concat($count,',',$first-val, ',',value)"/>
    

    To:

    <xsl:value-of select="concat(position(),',',$first-val, ',',value)"/>
    

    When the corrected transformation is applied to the provided XML document, the wanted result is produced.

    0 讨论(0)
  • 2021-01-17 04:46

    Can you show me your input xml and desired output xml?

    I kind of cringe a little when I see a foreach in xsl - it's a template language, and rarely needs foreach...

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE stylesheet [
        <!ENTITY comma "<xsl:text>,</xsl:text>">
        <!ENTITY cr "<xsl:text>
    </xsl:text>">
    ]>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    >
        <xsl:output method="text" indent="no" />
        <xsl:template match="/">
            <xsl:apply-templates select="/swift/message/block4/tag [name='77']"/>
        </xsl:template>
    
        <xsl:template match="message/block4/tag [name='77']">
            <xsl:apply-templates select="../../block2/@type"/>
            <xsl:value-of select="../../block2/messageType"/>
            <xsl:value-of select="../../block2/messagePriority"/>&comma;
            <xsl:number format="000001"/>&comma;
            <xsl:value-of select="../../block3/tag [name='32']/value"/>&comma;
            <xsl:value-of select="value"/>&cr;
        </xsl:template>
    
        <xsl:template match="@type[.='input']">O</xsl:template>
    
        <xsl:template match="@type[.='output']">I</xsl:template>
    
        <xsl:template match="text()"/>
    
    </xsl:stylesheet>
    

    You need one row for each block4 name. So apply a template for that block4/tag [name='77']

    Then - for every one of those, select the parent elements that you need.

    xsl:number will count the number of times it selected.

    The ENTITY items are there to control whitespace - otherwise the formatting is crap.

    No need for a foreach. Hope this helps

    0 讨论(0)
提交回复
热议问题