Showing posts with label xml Using XSL transforms to compare two nodes and then delete one. Show all posts
Showing posts with label xml Using XSL transforms to compare two nodes and then delete one. Show all posts

Using XSL transforms to compare two nodes and then delete one

Tuesday, November 3, 2015

XSLT newbie - I'm trying to use XSL transforms to compare and merge XML data from two different locations.



The data currently looks like this:



<root>
<table1>
<product>12345</product>
<dateCreated>2015-06-04</dateCreated>
<dateCompleted></dateCompleted>
</table1>
<table2>
<dateCreated>2015-08-28T06:34:00</dateCreated>
<dateCompleted></dateCompleted>
</table2>
</root>


and this is how it needs to be:



<root>
<newtable>
<product>12345</product>
<dateCreated>2015-08-28T06:34:00</dateCreated>
<dateCompleted></dateCompleted>
</newtable>
</root>


Essentially, I need to check if /table2/dateCreated exists, and has a value, and if yes, put that node in the final XML.



The XSL file looks like this:



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="table1/dateCreated | table2/dateCreated">
<xsl:if test="table2/dateCreated !=''">
<xsl:copy>
<xsl:apply-templates select="table2/dateCreated"/>
</xsl:copy>
</xsl:if>
</xsl:template>

</xsl:stylesheet>


Obviously I'm at a loss as to what is happening - any assistance is appreciated! Thank you!