I’m trying to convert XML to XML using XSLT. The output XML should be sorted based on ModificationTime element in the input XML.
Below is the xml code.
<?xml version="1.0" encoding="UTF-8"?>
<Process>
<currentDayAndHour>@Fri16</currentDayAndHour>
<!-- Few elements here. Need to retain them -->
<rowCount>1</rowCount>
<currentRow>1</currentRow>
<ClientList>
<Status>0</Status>
<ServerResponse>
<Code>0</Code>
<Text>OK</Text>
</ServerResponse>
<ServiceStartTime>2012-11-09 16:06:42.786</ServiceStartTime>
<ServiceEndTime>2012-11-09 16:06:42.827</ServiceEndTime>
<Files>
<File>
<Name>test.20121107215230411.txt</Name>
<Size>29</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352343152</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
<File>
<Name>test.20121107183757513.txt</Name>
<Size>29</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352331478</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
<File>
<Name>test1.20121107215230500.txt</Name>
<Size>32</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352343152</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
<File>
<Name>test1.txt</Name>
<Size>32</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352323788</ModificationTime>
<Owner>65174</Owner>
<Group>75431</Group>
</File>
<File>
<Name>HMP_test.txt</Name>
<Size>28</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352199478</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
<File>
<Name>test1.20121107183757585.txt</Name>
<Size>32</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352331478</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
<File>
<Name>client_access.20121108101411179.txt</Name>
<Size>4182</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352387653</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
<File>
<Name>TechMtngAgenda.txt</Name>
<Size>107</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1352044842</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
<File>
<Name>test.txt</Name>
<Size>29</Size>
<Type>Regular</Type>
<Permissions>-rw-r--r--</Permissions>
<ModificationTime>1350063313</ModificationTime>
<Owner>19737</Owner>
<Group>70902</Group>
</File>
</Files>
</ClientList>
<currentDocument>1</currentDocument>
</Process>
I need the output XML with all the input elements but Files tag should contain each File in the increasing order of ModificationTime. I’m kindof new to XSLT. I tried using xsl:sort but not able to get desired result.
This stylesheet will do what you want in XSLT 1.0
This stylesheet will do what you want in XSLT 2.0
In both cases the Identity template is used to copy the document exactly and a single
xsl:templateis introduced to override the the handling of theFileselement. In the XSLT 1.0 examplexsl:apply-templatesis used withxsl:sortto sort theFileelements byModificationTime. The<xsl:apply-templates select="File">the handling of the sortedFileelements is passed back to the Identity template so it could potentially be overridden further.In the XSLT 2.0 example
xsl:sequenceis used to directly insert the input node into the result tree. Likewisexsl:perform-sortreturns the sorted sequence directly rather than performing additional instructions to copy the element. Note that these changes will probably make the stylesheet faster to execute but also reduce the flexibility of future maintenance. It’s much harder to add overrides when you are selecting things directly. The XSLT 1.0 stylesheet or it’s style of handling could be done in XSLT 2.0 without major changes. One final note, both of these examples omit anynode()children ofFilesthat is not aFileelement. To capture those you could add in XSLT 1.0Or in XSLT 2.0 only
or