这是我正在尝试实现的: 我有一个CSV文件,其数据类似1,4,5 ..等等(不是一个固定的系列),我有一个XML是有某些节点重复。现在从该XML中,我需要删除所有那些位置在CSV文件中的节点。在XSLT中比较CSV
这就是我想要做到的: 我将CSV文件作为参数传递给XSLT并调用递归模板来打印XML。 (感谢我看到了很长一段时间back..don't记住地址后)
问题:“这是行不通的” :)
下面是我的示例XML和XSLT。任何帮助,将不胜感激。
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>
这里是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>
XSLT的任何原因?正如你发现的那样,XSLT不知道CSV,这使得CSV成为绝对的平衡器。所以通过C#/ Java/{chioce语言}中的DOM操作来实现这一点会更容易。 –
没有克里斯......其实我们并没有使用C#..但是我在JAVA中并没有那么高兴,所以如果你能提供相同的指针,我会很乐意浏览。 – Kapil
这就是为什么我在列表中添加了{语言的选择},因为几乎每个环境都有一个XML DOM解析器,这就是为什么我问你为什么只限于XSLT。 –