合并在UNIQUE ID中具有相同ID但包含所有子节点的节点的子节点

时间:2011-09-14 14:49:57

标签: xml xslt unique

同样是一个类似的问题,但现在结果包含多个孩子。

任何帮助?

我有这个:

<root>
<rowdata>
    <ID>1</ID>
    <pxPages>
        <rowdata>
            <comment>comment A</comment>
            <timestamp>timestamp A</timestamp>
            <userID>user A</userID>
        </rowdata>
    </pxPages>
</rowdata>
<rowdata>
    <ID>2</ID>
    <pxPages>
        <rowdata>
            <comment>comment B</comment>
            <timestamp>timestamp B</timestamp>
            <userID>user B</userID>
        </rowdata>
    </pxPages>
</rowdata>
<rowdata>
    <ID>2</ID>
    <pxPages>
        <rowdata>
            <comment>comment C</comment>
            <timestamp>timestamp C</timestamp>
            <userID>user C</userID>
        </rowdata>
    </pxPages>
</rowdata>

并寻找:

<root>
<ResultOperationalStatusCategory>
    <identifier>1</identifier>
    <comments>
        <comment>comment A</comment>
        <timestamp>timestamp A</timestamp>
        <userID>user A</userID>
    </comments>
</ResultOperationalStatusCategory>
<ResultOperationalStatusCategory>
    <identifier>2</identifier>
    <comments>
        <comment>comment B</comment>
        <timestamp>timestamp B</timestamp>
        <userID>user B</userID>
    </comments>
    <comments>
        <comment>comment C</comment>
        <timestamp>timestamp C</timestamp>
        <userID>user C</userID>
    </comments>
</ResultOperationalStatusCategory>

所以,结果是每个唯一标识符,但包含所有注释。

谢谢!

1 个答案:

答案 0 :(得分:0)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="k" match="root/rowdata" use="ID"/>

    <xsl:template match="/root">
        <xsl:copy>
            <xsl:apply-templates select="rowdata[generate-id(.) = 
                           generate-id(key('k', ID))]"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="rowdata">
        <ResultOperationalStatusCategory>
            <identifier>
                <xsl:value-of select="ID"/>
            </identifier>

            <xsl:apply-templates select="key('k', ID)/pxPages/rowdata"/>

        </ResultOperationalStatusCategory>
    </xsl:template>

    <xsl:template match="pxPages/rowdata">
        <comments>
            <xsl:apply-templates select="*"/>
        </comments>
    </xsl:template>

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

</xsl:stylesheet>