使用 XSLT 添加父节点以收集多个子节点

Add parent node to gather several children nodes using XSLT

正在尝试转换 XML 文件:

<collection>
<collectionDetails>
    <owner>David</owner>
    <creationDate>20140515</creationDate>       
    <movie>
        <title>The Little Mermaid</title>
        <stars>5</stars>
    </movie>
    <movie>
        <title>Frozen</title>
        <stars>3</stars>
    </movie>
</collectionDetails>    

进入 XML 文件:

<collection>
    <collectionDetails>
        <owner>David</owner>
        <creationDate>20140515</creationDate>
        <movies>
            <movie>
                <title>The Little Mermaid</title>
                <stars>5</stars>
            </movie>
            <movie>
                <title>Frozen</title>
                <stars>3</stars>
            </movie>
        </movies>
    </collectionDetails>    
</collection>

(ie) 我只是想向所有 "movie" 节点添加一个共同的父 "movies" 节点。

你有 XSLT 样式表来实现吗?

这是一种方法:

XSLT 1.0

<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="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="collectionDetails">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()[not(self::movie)]"/>
        <movies>
            <xsl:apply-templates select="movie"/>        
        </movies>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>