Reputation: 1391
I want to modify the following xml using XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<folder>
<CRDATTIM>2012-08-31-08.26.11.805400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-03-26-05.22.22.193840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
<folder>
<CRDATTIM>2012-11-06-23.57.08.089400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-04-29-06.58.32.992840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-04-29-06.58.31.305840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
</response>
Now, I want to add a new node <sort>
to the root node <response>
such that the node <sort>
will contain two children <field />
and <ascending />
.But, here for each case in the above xml, the two tags <field />
and <ascending />
should be added to the <sort>
node.For example in the above xml, there are two cases(<case>
). So, the node <sort>
should have two <field />
tags and <ascending />
tags. Ultimately, My final xml should look like below:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<folder>
<CRDATTIM>2012-08-31-08.26.11.805400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-03-26-05.22.22.339840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-03-26-05.22.22.193840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
<folder>
<CRDATTIM>2012-11-06-23.57.08.089400</CRDATTIM>
<RECORDCD>F</RECORDCD>
</folder>
<case>
<CRDATTIM>2014-04-29-06.58.32.992840</CRDATTIM>
<RECORDCD>C</RECORDCD>
<issue>
<KEY>2014-04-29-06.58.31.305840T01</KEY>
<PRTY>999</PRTY>
</issue>
</case>
<sort>
<field />
<ascending />
<field />
<ascending />
<field />
</sort>
</response>
Please share your thoughts on how to achieve the desired functionality. Please let me know if my question is not clear. Thanks in advance.
Upvotes: 1
Views: 35
Reputation: 4739
You can use an identity template to copy the original XML and put a <sort>
element on the end. See next tutorial: http://www.xmlplease.com/xsltidentity
Here is an XSLT example:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="response">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<sort>
<xsl:for-each select="case">
<field />
<ascending />
</xsl:for-each>
</sort>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2