开发者

merge childs of nodes that have same ID in a UNIQUE ID but containing all childs

Again a similar question but now the result is containing multiple childs.

any help?

I have this:

<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>

and looking for:

<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>

so, result is per unique identifier but containing all comments.

thanks!


<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜