Jump to content
Sign in to follow this  
Hari Arun

How to replace the nodes based on attribute value in xml using xslt.

Recommended Posts

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...