Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 21st, 2014, 09:56 AM
Registered User
 
Join Date: Sep 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select nodes whose attribute matches another node's attribute? nikolai XSLT 5 April 18th, 2018 11:30 AM
XSLT:How to loop through nodes and retrieve attributes values based on some condition smandeh XSLT 2 March 24th, 2009 01:50 PM
Sorting some nodes by attribute value acw274 XSLT 8 July 2nd, 2008 01:22 AM
Replace an attribute with another attribute georgemeng XSLT 8 June 10th, 2008 11:04 AM
XPath - Selecting nodes based on attribute values billy_bob_the_3rd XML 4 December 1st, 2004 06:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.