0

Hi,
My xml has got several tags with same name. I am trying to pick out only top 5 tags. I initially thought this would be simple and tried, but after spending so much time I couldn't get the desired output. Any help would be greatly appreciated. The scenario is detailed below.

My input file:

<input version="1-1">
    <events>
        <primary>
            <action>insert</action>
            <object href="#operation1">
                <objectNature>operation</objectNature>
                <objectId>404</objectId>
            </object>
            <inputBy>
                <userName>ABC</userName>
                <dept>sales</dept>
            </inputBy>
        </primary>
        <secondary>
            <action>accumulate</action>
            <object href="#res_1">
                <objectNature>tree</objectNature>
                <objectId>111</objectId>
            </object>
            <inputBy>
                <userName>ABC</userName>
                <dept>sales</dept>
            </inputBy>
        </secondary>
        <secondary>
            <action>accumulate</action>
            <object href="#res_2">
                <objectNature>tree</objectNature>
                <objectId>222</objectId>
            </object>
            <inputBy>
                <userName>ABC</userName>
                <dept>sales</dept>
            </inputBy>
        </secondary>
        ..
        ..
        ..
        ..
        <secondary>
        </secondary>
    </events>
</input>

As shown in the input file, a lot of 'secondary' tags will be present. I just want to take only top 5 tags or in general, tags present in any desired position.

desired output:

<input version="1-1">
    <events>
        <primary>
            <action>insert</action>
            <object href="#operation1">
                <objectNature>operation</objectNature>
                <objectId>404</objectId>
            </object>
            <inputBy>
                <userName>ABC</userName>
                <dept>sales</dept>
            </inputBy>
        </primary>
        <secondary>
            <action>accumulate</action>
            <object href="#res_1">
                <objectNature>tree</objectNature>
                <objectId>111</objectId>
            </object>
            <inputBy>
                <userName>ABC</userName>
                <dept>sales</dept>
            </inputBy>
        </secondary>
        </events>
</input>

the code which i was trying with is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"></xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/input/events/secondary">
        <xsl:for-each select=".">
            <xsl:if test="position()&lt;=5">
                <xsl:copy-of select="."></xsl:copy-of>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Can anybody please correct me where I am doing wrong?

Thanks a lot for your help...

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by Mike Askew
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.