I have a following XML
<Root>
<Element A/>
<Element B/>
<Data1>
<DataElement/>
<Values>
<Value>2222</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>ABC</Value>
<Name>field2</Name>
</Values>
</Data1>
<Data2>
<DataElement/>
<Values>
<Value>1111</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>XYZ</Value>
<Name>field2</Name>
</Values>
</Data2>
<DataN>
...
</DataN>
</Root>
I need to get the same XML with Data section sorted by "Value" of specified field name, for exmple: sort by "field1" will return
<Root>
<Element A/>
<Element B/>
<Data2>
<DataElement/>
<Values>
<Value>1111</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>XYZ</Value>
<Name>field2</Name>
</Values>
</Data2>
<Data1>
<DataElement/>
<Values>
<Value>2222</Value>
<Name>field1</Name>
</Values>
<Values>
<Value>ABC</Value>
<Name>field2</Name>
</Values>
</Data1>
<DataN>
...
</DataN>
</Root>
Also, I have to send name of sort field as parameter...
I think you can go with this simple transform:
When applied to your input (corrected to make it well-formed):
produces:
Explanation:
This transformation implements exactly the stated requirements. It takes special care to preserve the exact order of the elements that are not to be sorted. No other answer at present does this:
When applied on the following XML document (the same as the provided one, but with an additional
<Element C=""/>
inserted betweenData1
andData2
so that we can verify the preservation of ordering of the non-sorted elements):produces the wanted, correct result -- note that the position of
<Element C=""/>
is preserved: