This question is similar to merge two elements using xslt based on attribute values but trying to put this in different way where it could understand better.
I have an xml file with two elements whose names are same, but second element is part of first element. Example:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- This is first element-->
<book>
<mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops">
<attribute name="bookprops">
abc.mybook.onebook=@Value@
def.mybook.twobook=@Value@
ghi.myebook.threebook=@Value@
</attribute>
</mbean>
<!--This is second element and is part of first element-->
<book>
<mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops">
<attribute name="bookprops">
abc.mybook.onebook=@New_Value@
def.mybook.twobook=@New_Value@
ghi.myebook.fourbook=@Value@
</attribute>
</mbean>
</book><!--closing tag of second element-->
</book><!--closing tag of first element-->
Objective is:
Combine both elements to one element and if both elements are having similar nodes with value of the nodes are different replace the values of first attribute nodes with second attribute nodes.
Procedure that i am thinking of:
- Sort the nodes of first and second elements.
- Split the nodes of first element and assign it to the variables. Example: node of first element abc.mybook.onebook=@Value@ is split to two and assigned them to variables varaible1=abc.mybook.onebook and varaible2=@Value@.
- Simillar way Split the nodes of second element and assign them to variables. Example node of second element abc.mybook.onebook=@New_Value@ is split to two and assigned them to variables as varaible3=abc.mybook.onebook and varaible4=@New_Value@.
- Now match the first element variables with second element variables. Example: if variable1 equals variable3 then replace variable2 with varaible4; else copy first element node and copy second element node.
This can be done very easily in shell or bash but since the requirement is to do with xslt, i am trying to figure it out how can this be accomplished.
The final output that i am expecting is:
<book>
<mbean code="org.book.mybooks" name="mycompany.props.jndi:name=mybookprops">
<attribute name="bookprops">
abc.mybook.onebook=@New_Value@
def.mybook.twobook=@New_Value@
ghi.myebook.threebook=@Value@
ghi.myebook.fourbook=@Value@
</attribute>
</mbean>
</book>