I have two XML files:
data1.xml
<?xml version="1.0" encoding="UTF-8"?>
<tables>
<table>
<row>
<cell colname="1">A</cell>
<cell colname="2">
<carType>Sedan</carType>
<gasType>Gasoline</gasType>
</cell>
<cell colnane="4">B</cell>
<cell colname="5">C</cell>
</row>
<row>
<cell colname="1">A1</cell>
<cell colname="2">
<carType>Truck</carType>
<gasType>Diesel</gasType>
</cell>
<cell colname="4">B1</cell>
<cell colname="5">C1</cell>
</row>
</table>
</tables>
data2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tables>
<table>
<row>
<cell colname="1">A</cell>
<cell colname="2">
<carType>SedanXYZ</carType>
<gasType>GasolineXYZ</gasType>
</cell>
<cell colname="4">B</cell>
<cell colname="5">C</cell>
</row>
<row>
<cell colname="1">A2</cell>
<cell colname="2">
<carType>Motorcycle</carType>
<gasType>Gasoline</gasType>
</cell>
<cell colname="4">U</cell>
<cell colname="5">Z</cell>
</row>
<row>
<cell colname="1">A1</cell>
<cell colname="2">
<carType>TruckXYZ</carType>
<gasType>DieselXYZ</gasType>
</cell>
<cell colname="4">B1</cell>
<cell colname="5">C1</cell>
</row>
</table>
</tables>
Basically I want to copy whatever contained in colname="2" from data2.xml to data1.xml and keep the rest of the data in data1.xml the same. The keys to find the equality are colname="4" and colname="5". My XSLT looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>
<xsl:param name="doc2"/>
<xsl:template match="/">
<xsl:message>Starting off</xsl:message>
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="cell[@colname='2']">
<xsl:variable name="key-value">
<xsl:call-template name="key-value"/>
</xsl:variable>
<xsl:for-each select="document($doc2)//row">
<xsl:if test="key('keyx', $key-value)">
<xsl:copy-of select="cell[@colname='2']"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Just copy any other elements, attributes, etc. -->
<xsl:template match="@*|node()" >
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="keyx" match="row"
use="concat(cell[@colname='4'], cell[@colname='5'])"/>
<!-- This template retrives the key value for an element -->
<xsl:template name="key-value">
<xsl:value-of select="concat(../cell[@colname='4'],../cell[@colname='5'])"/>
</xsl:template>
</xsl:stylesheet>
The result I'm expecting:
<?xml version="1.0" encoding="iso-8859-1"?>
<tables>
<table>
<row>
<cell colname="1">A</cell>
<cell colname="2">
<carType>SedanXYZ</carType>
<gasType>GasolineXYZ</gasType>
</cell>
<cell colname="4">B</cell>
<cell colname="5">C</cell>
</row>
<row>
<cell colname="1">A1</cell>
<cell colname="2">
<carType>TruckXYZ</carType>
<gasType>DieselXYZ</gasType>
</cell>
<cell colname="4">B1</cell>
<cell colname="5">C1</cell>
</row>
</table>
</tables>
BUT I'm getting incorrect output like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<tables>
<table>
<row>
<cell colname="1">A</cell>
<cell colname="2">
<carType>SedanXYZ</carType>
<gasType>GasolineXYZ</gasType>
</cell>
<cell colname="2">
<carType>Motorcycle</carType>
<gasType>Gasoline</gasType>
</cell>
<cell colname="2">
<carType>TruckXYZ</carType>
<gasType>DieselXYZ</gasType>
</cell>
<cell colname="4">B</cell>
<cell colname="5">C</cell>
</row>
<row>
<cell colname="1">A1</cell>
<cell colname="2">
<carType>SedanXYZ</carType>
<gasType>GasolineXYZ</gasType>
</cell>
<cell colname="2">
<carType>Motorcycle</carType>
<gasType>Gasoline</gasType>
</cell>
<cell colname="2">
<carType>TruckXYZ</carType>
<gasType>DieselXYZ</gasType>
</cell>
<cell colname="4">B1</cell>
<cell colname="5">C1</cell>
</row>
</table>
</tables>
So several questions:
- What's wrong with my XSLT ?
What's the technique to debug the key calls ?.
Thanks you!.
John