I want to transform the data received in one XML into another XML file using XSLT.
The data in XML to be parsed contains "_" (underscore). I want to generate elements from this XML and group them based on the names extracted from element names.
XML to be parsed
<data>
<fruits_apple_red>1</fruits_apple_red>
<animal_carnivorous_cat_tiger_white>5</animal_carnivorous_cat_tiger_white>
<animal_reptiles_lizard>3</animal_reptiles_lizard>
<animal_carnivorous_lion>4</animal_carnivorous_lion>
<fruits_orange_orange>2</fruits_orange_orange>
<animal_carnivorous_cat_hyena>6</animal_carnivorous_cat_hyena>
</data>
XML needed
<?xml version="1.0" encoding="UTF-8"?>
<data>
<fruits>
<apple_red>1</apple_red>
<orange_orange>2</orange_orange>
</fruits>
<animal>
<carnivorous>
<cat>
<tiger_white>5</tiger_white>
<hyena>6</hyena>
</cat>
<lion>4</lion>
</carnivorous>
<reptiles_lizard>3</reptiles_lizard>
</animal>
</data>
I'm new to XSLT and I have tried using XSLT string functions(substring) but unable to get XML in this format. The problem I faced is that the element names are dynamic and we need to extract names out the element names. So, I can't really write code with name "data" as root element or "animal/fruits" as 2nd level element names.
Another thing that we need is the multi level (like for animal) although I feel that this may require some hardcoded checks(using xsl:if statement) at some stage. But the point is too group the data multi-level as far as possible.
Could anyone please let me know how can this can be done either using XSLT.
Note: This is sample data but real data contains element names separated by "_". Client needs it this way so that they can directly bind the required part to respective grid.