0

My xml is as given below :

<items>
    <item id='10' name='item1'/>
    <item id='20' name='item2'/>
    <item id='30' name='item3'/>
     <item id='40' name='item4'/>
    </items>

    <parent_group>
    <parent_group_item item_id='10' parent_group_id='30'/>
    <parent_group_item item_id='20' parent_group_id='30'/>
    </parent_group>

I want to convert the above code snippet to the below format using xsl:

  <items>
    <item>
    <id>10</id>
    <name>    item1    </name>
    <parent_group_id>30</parent_group_id>
    </item>
    <item>
    <id>20</id>
    <name>    item2    </name>
    <parent_group_id>30</parent_group_id>
    </item>
    <item>
    <id>30</id>
    <name>    item3    </name>
   
    </item>
    <item>
    <id>40</id>
    <name>    item4   </name>
   
    </item>
    </items>

could you please help i need it using xslt only.and i cannot hard code any values as the values are very dynamic.

6
  • 1
    The example is a bit thin. Can one assume all the required items are found inside the <parent_group>? And are you sure you want to lose the name/s of the parent item/s? --- Note also that your inout example is missing a root element!. Commented Oct 21, 2014 at 18:58
  • No all the items would not be part of a <parent_group> as some items would be independent on their own.Yes the parent item names are not important in this example.So The independent items would not have a <parent_group> element in them. Commented Oct 21, 2014 at 19:05
  • "No all the items would not be part of a <parent_group> as some items would be independent on their own." Please post an example showing that. Commented Oct 21, 2014 at 19:06
  • This question appears to be off-topic because it is about a request for code. Commented Oct 21, 2014 at 19:12
  • 1
    @torazaburo I have rolled back your edit. I don't think this is the proper way to make your views known. Commented Oct 21, 2014 at 19:34

1 Answer 1

1

Assuming your input Xml Looks like so (i.e. has a root element):

<xml>
    <items>
    <item id='10' name='item1'/>
    <item id='20' name='item2'/>
    <item id='30' name='itemParent'/>
    </items>

    <parent_group>
    <parent_group_item item_id='10' parent_group_id='30'/>
    <parent_group_item item_id='20' parent_group_id='30'/>
    </parent_group>
</xml>

You can then use key to lookup the item_ids in the parent_group_item elements

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:key name="parentGroup" match="parent_group_item" use="@item_id" />

    <xsl:template match="xml/items">
        <items>
            <xsl:apply-templates select="item"></xsl:apply-templates>
        </items>
    </xsl:template>

    <xsl:template match="item">
        <item>
            <id>
                <xsl:value-of select="@id"/>
            </id>
            <name>
                <xsl:value-of select="@name"/>
            </name>
        <xsl:variable name="parentGroupKey" select="key('parentGroup', @id)" />
        <xsl:if test="$parentGroupKey">
            <parent_group_id>
                <xsl:value-of select="key('parentGroup', @id)/@parent_group_id"/>
            </parent_group_id>
        </xsl:if>
        </item>
    </xsl:template>
</xsl:stylesheet>

Updated to omit the parent_group_id element if it is unmatched.

Sign up to request clarification or add additional context in comments.

3 Comments

You should define the key to match="parent_group_item" use="@item_id". Then use <xsl:value-of select="key('parentGroup', @id)/@parent_group_id"/> to get the parent id value. Currently, your result is incorrect.
@michael.hor257k Thanks very much - I wasn't paying enough attention to my code.
Thanks Stuart,Micahel made the appropriate changes and fixed the code thanks a ton.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.