使用 XSLT 将 XML 个节点转换为父节点的属性并合并重复节点

Transform XML nodes into attributes of the parent and merge duplicate nodes using XSLT

我需要使用 XSLT 将 XML 提要转换为:

这里是 XML:

<item>
    <guid>XX-12345</guid>
    <link>http://www.whosebug.com.au</link>
    <title>This is the title</title>
    <enclosure type="image/jpeg" length="0" url="http://www.whosebug.com.au/images/image.jpg"/>
    <description>Description of my article</description>
    <pubDate>2015-05-13T12:05:33.620000+10:00</pubDate>
    <section>test</section>
    <section>other</section>
    <section>sub-other</section>
    <section>2015</section>
    <section>05</section>
    <category>parentX:childA:category text</category>
    <category>parentY:childB:category text, with commas</category>
    <category>parentZ:childC:category text</category>
</item>

这是期望的输出:

<item guid="XX-12345" link="http://www.whosebug.com.au" title="This is the title" image_url="http://www.whosebug.com.au/images/image.jpg" description="Description of my article" pubDate="2015-05-13T12:05:33.620000+10:00" section="'test','other','sub-other','2015','05'" category="'parentX:childA:category text','parentY:childB:category text, with commas','parentZ:childC:category text'"></item>

这个 XSLT 帮助我完成了大部分工作,但我不知道如何创建以逗号分隔的重复节点列表(这导致 <item section="05"> 上面的多个部分 XML):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="channel">
    <channel>
      <xsl:apply-templates/>
    </channel>
  </xsl:template>

  <xsl:template match="item">
    <item>
      <xsl:for-each select="*">
        <xsl:choose>
            <xsl:when test="name() = 'enclosure'">
                <xsl:attribute name="image_url">
                  <xsl:value-of select="@url"/>
                </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:attribute name="{name()}">
                    <xsl:value-of select="text()"/>
                </xsl:attribute>
            </xsl:otherwise>        
        </xsl:choose>
      </xsl:for-each>
    </item>
  </xsl:template>
</xsl:stylesheet>

谢谢!

假设 XSLT 2.0 item 的主模板可以是:

<xsl:template match="item">
    <xsl:copy>
        <xsl:for-each-group select="*" group-by="node-name(.)">
            <xsl:choose>
                <xsl:when test="self::enclosure">
                    <xsl:attribute name="image_url" select="@url"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:attribute name="{name()}"
                      select="if (current-group()[2]) then current-group()/concat('''', ., '''') else ."
                      separator=","/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>