XSLT Concat fields together

后端 未结 1 745
生来不讨喜
生来不讨喜 2021-01-19 02:17

i am trying to filter on a specific field and concat on another field:

Input:



    &         


        
相关标签:
1条回答
  • 2021-01-19 02:51

    Use this template:

    <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:key name="k" match="payload" use="concat(firstname, '|', secondname)"/>
    
        <xsl:template match="payload[generate-id() = 
                      generate-id(key('k', concat(firstname, '|', secondname)))]">
            <xsl:copy>
                <xsl:copy-of select="firstname"/>
                <xsl:copy-of select="secondname"/>
                <number>
                    <xsl:for-each select="key('k', concat(firstname, '|', secondname))">
                        <xsl:value-of select="number"/>
    
                        <xsl:if test="position() != last()">
                            <xsl:text>,</xsl:text>
                        </xsl:if>
                    </xsl:for-each>
                </number>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="payload"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    When applied to provided input XML, it outputs wanted correct result:

    <payloads>
      <payload>
        <firstname>michael</firstname>
        <secondname>brown</secondname>
        <number>1,2,3</number>
      </payload>
    </payloads>
    
    0 讨论(0)
提交回复
热议问题