I am working over sorting the Grand Parent based on a field in Father. The source file looks like
<Root>
<AllData>
<Data_not_to_be_sorted>
<Additional_data1>
<Some_test_data1/>
<Some_test_data2/>
</Additional_data1>
</Data_not_to_be_sorted>
<RealData>
<Some_data1/>
<Some_data2/>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>4</Value>
<Name>name in 4</Name>
</Father>
</GrandFather>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>3</Value>
<Name>name in 3</Name>
</Father>
</GrandFather>
</RealData>
<RealData>
<Some_data1/>
<Some_data2/>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>2</Value>
<Name>name in 2</Name>
</Father>
</GrandFather>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>1</Value>
<Name>name in 1</Name>
</Father>
</GrandFather>
</RealData>
</AllData>
</Root>
The XSLT code, I am using is as below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RealData">
<xsl:copy>
<xsl:apply-templates select="Data_required_as_it_is"></xsl:apply-templates>
<xsl:apply-templates select="GrandFather">
<xsl:sort select="Father/Value" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The code is working well…My query is – Can I write a generic line for
Data_required_as_it_is. Imagine a situation where I have 20 different xml tags for “Data_required_as_it_is” so either I need write all of them manually or just write them in a generic way…
The output of the code is as below:
<Root>
<AllData>
<Data_not_to_be_sorted>
<Additional_data1>
<Some_test_data1/>
<Some_test_data2/>
</Additional_data1>
</Data_not_to_be_sorted>
<RealData>
<Some_data1/>
<Some_data2/>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>3</Value>
<Name>name in 3</Name>
</Father>
</GrandFather>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>4</Value>
<Name>name in 4</Name>
</Father>
</GrandFather>
</RealData>
<RealData>
<Some_data1/>
<Some_data2/>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>1</Value>
<Name>name in 1</Name>
</Father>
</GrandFather>
<GrandFather>
<Data_required_as_it_is></Data_required_as_it_is>
<Father>
<Value>2</Value>
<Name>name in 2</Name>
</Father>
</GrandFather>
</RealData>
</AllData>
</Root>
One could use a more generic code like this:
Note: This will sort any any non-
GrandFathernode before anyGrandFathernode and will do the specified sorting ofGrandFatherelements by theirFather/Valueelements.In case it is desired that the non-
GrandFatherelements remain interspersed within theGrandFatherelements, this can also be accomplished — ask another question if interested.