How to replace the nodes based on attribute using xslt
Below is the XML : How to replace the nodes in XML based on attribute using xslt.
<?xml version="1.0" encoding="UTF-8"?>
<DST>
<CDGCreateTree>
<DST>
<currentJobName>PQContactComplete</currentJobName>
<AWD>
<case>
<transaction>
<UIID>Test</UIID>
</transaction>
</case>
<case>
<transaction>
<UIID>Testsample</UIID>
</transaction>
</case>
</AWD>
</DST>
</CDGCreateTree>
<newNode>
<transaction relate="Y" id="1"/>
</newNode>
</DST>
1)In above xml total 2 cases which is having individual transactions
2)newNode node contains another transaction with ID(id = "1")
My requirement is like i want to replace first case transaction with newNode transaction which is generated based
on first case transaction only, That is the reason why i am looking into remove unnecessary data.
3)For every operation newNode contains only one transaction.
Result should be:
<?xml version="1.0" encoding="UTF-8"?>
<DST>
<CDGCreateTree>
<DST>
<currentJobName>PQContactComplete</currentJobName>
<AWD>
<case>
<transaction relate="Y" id="1"/>
</case>
<case>
<transaction>
<UIID>Testsample</UIID>
</transaction>
</case>
</AWD>
</DST>
</CDGCreateTree>
</DST>
In future I may get another transaction with id under newNode node below is the xml
<?xml version="1.0" encoding="UTF-8"?>
<DST>
<CDGCreateTree>
<DST>
<currentJobName>PQContactComplete</currentJobName>
<AWD>
<case>
<transaction relate="Y" id="1"/>
</case>
<case>
<transaction>
<UIID>Testsample</UIID>
</transaction>
</case>
</AWD>
</DST>
</CDGCreateTree>
<newNode>
<transaction relate="Y" id="2"/>
</newNode>
</DST>
This time I should replace second case transaction with newNode transaction(becasue first case transaction already replaced with newNode transaction and it contains id value)
like that I want continue the process.
Finally Expected result:
<?xml version="1.0" encoding="UTF-8"?>
<DST>
<CDGCreateTree>
<DST>
<currentJobName>PQContactComplete</currentJobName>
<AWD>
<case>
<transaction relate="Y" id="1"/>
</case>
<case>
<transaction relate="Y" id="2"/>
</case>
</AWD>
</DST>
</CDGCreateTree>
</DST>
I have used below xslt, but it's not helped me, Please can you give any suggestions to me
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:variable name="targetNode" select="//newNode/transaction"/>
<xsl:template match="(//transaction[ancestor::*[name() = "CDGCreateTree"]][not(@id)])[1]">
<xsl:copy-of select="$targetNode"/>
</xsl:template>
</xsl:stylesheet>
Explaination of Above XSL:
(//transaction[ancestor::*[name() = 'CDGCreateTree']][not(@id)])[1] this statement is defines about case transaction which is not having id and first transaction replace with (//newNode/transaction) New transaction.
In future if i get any newNode transaction it should be replace accordingly to above xsl,i thought it will work as per the statemnt but some thing is missed please suggest me.
Last edited by Arun Hari; September 21st, 2014 at 10:04 AM..
|