添加父节点以使用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>

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

您有一个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>

最新更新