#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Output specific nodes and child elements and delete other parent and child nodes


    Hi Experts!

    Can you help suggest how I can insert Returned tag(under secondary) on the Inventory tag(under Primary)
    and then output only the Profile and all its aattributes under Primary as below.

    Input:

    <MergedProfile>
    <Primary>
    <Profile>
    <Primary>
    <Books></Books>
    <Magazines></Magazines>
    </Primary>
    <Inventory action="mod">
    <Borrowed action="mod">
    <January>24</January>
    </Borrowed>
    </Inventory>
    </Profile>
    </Primary>

    <Secondary>
    <Profile>
    <Inventory action="mod">
    <Returned>
    <January>7</January>
    </Returned>
    </Inventory>
    </Profile>
    </Secondary>
    </MergedProfile>


    Output Desired:
    <Profile>
    <Primary>
    <Books></Books>
    <Magazines></Magazines>
    </Primary>
    <Inventory action="mod">
    <Borrowed action="mod">
    <January>24</January>
    </Borrowed>
    <Returned action="del">
    <January>7</January>
    </Returned>
    </Inventory>
    </Profile>

    I have the below code playing into - it inserts but copying all.
    Have changed the template match and apply-templates to /MergedProfile/Primary but it either returns nothing or everything

    <xsl:stylesheet version="1.0">
    <xslutput method="xml" version="1.0"/>

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

    <xsl:template match="/MergedProfile/Primary/Profile/Inventory">
    <xsl:element name="Returned">
    <xsl:attribute name='action'>del</xsl:attribute>
    <xsl:for-each select="../../../Secondary/Profile/Inventory/Returned/January">
    <xsl:element name="January">
    <xsl:value-of select="."/>
    </xsl:element>
    </xsl:for-each>
    </xsl:element>
    </xsl:template>

    </xsl:stylesheet>

    Thanks! Appreciate your help. Can't find any sites related to this...
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0
    I'm closing the thread cause I got the desired output already
    Just to share what I did,

    Adding to my existing code that I was playing with,
    I removed the Secondary node and all its contents
    <xsl:template match="Secondary"></xsl:template>


    and remove tag MergedProfile and Primary, but retaining its contents
    <xsl:template match="Primary">
    <xsl:apply-templates select="node()"/>
    </xsl:template>

    <xsl:template match="MergedProfile">
    <xsl:apply-templates select="node()"/>
    </xsl:template>

IMN logo majestic logo threadwatch logo seochat tools logo