This is what I am trying to achieve:
I have a CSV file which has data like 1,4,5..and so on(not a fixed series) and I have an XML which is having certain node repeating. Now from that XML I need to remove all those nodes whose position is present in the the CSV file.
This is how I am trying to do it:
I am passing the CSV file as parameter to XSLT and calling a recursive template to print the XML. (Thanks to a post I had seen long time back..don’t remember the address)
Problem: “This is not working” 🙂
Below is my sample XML and XSLT. Any help would be appreciated.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item IItemID="">
</Item>
<Item ItemID="100-8754">
</Item>
<Item ItemID="206-4141">
</Item>
<Item ItemID="">
</Item>
</Items>
Here is the XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:param name="ErrorPos" as="xs:string" select="'1,4'"/>
<xsl:template match="*|/">
<xsl:call-template name="commaSplit">
<xsl:with-param name="dataString" select="$ErrorPos"/>
<xsl:with-param name="position" select="1"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="commaSplit">
<xsl:param name="dataString"/>
<xsl:param name="position"/>
Vivek
<xsl:choose>
<xsl:when test="contains($dataString,',')">
<!-- Select the first value to process -->
<xsl:call-template name="getItems">
<xsl:with-param name="errorPosition" select="substring-before($dataString,',')"/>
<xsl:with-param name="position" select="$position"/>
</xsl:call-template>
<!-- Recurse with remainder of string -->
<xsl:call-template name="commaSplit">
<xsl:with-param name="dataString" select="substring-after($dataString,',')"/>
<xsl:with-param name="position" select="$position + 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- This is the last value no need to recurse -->
<xsl:call-template name="getItems">
<xsl:with-param name="errorPosition" select="$dataString"/>
<xsl:with-param name="position" select="$position"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Process of individual value here -->
<xsl:template name="getItems" match="/">
<xsl:param name="errorPosition"/>
<xsl:param name="position"/>
<Items>
<xsl:value-of select="$errorPosition"/>
<xsl:value-of select="concat(',',$position)"/><!--Just for testing...will be replaced by copy statement-->
</Items>
</xsl:template>
</xsl:stylesheet>
your problem is because you have 2 templates which both match “/” the root of the xml document, in order to funnel the data correctly you must ensure a priority is set on the template you wish to execute first! since you are using named templates you can simply remove the match attribute off the getItems template!
with this minor change it works, unless you have a different output desired?
Regards,
Sam