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

时间:2015-05-27 09:20:48

标签: xslt-1.0

尝试转换该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>

(即)我只想添加一个共同的父母&#34;电影&#34;节点到所有&#34;电影&#34;节点

你有一个XSLT样式表来实现吗?

1 个答案:

答案 0 :(得分:1)

这是一种方式:

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>