XSLT Concat fields together

丶灬走出姿态 提交于 2019-12-01 17:52:40

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!