Hari Arun Posted September 22, 2014 Share Posted September 22, 2014 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 transactions2)newNode node contains another transaction with ID(id = "1") 3)My requirement is like i want to replace first case transaction with newNode transaction which is generated basedon first case transaction only, That is the reason why i am looking into remove unnecessary data. 4)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, Can any one please suggest me or correct 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now