【发布时间】:2012-01-18 07:53:04
【问题描述】:
这就是我想要实现的目标: 我有一个 CSV 文件,其中包含 1、4、5 等数据(不是固定系列),并且我有一个 XML,其中有某些节点重复。现在,我需要从该 XML 中删除其位置存在于 CSV 文件中的所有节点。
这就是我尝试的方式: 我将 CSV 文件作为参数传递给 XSLT 并调用递归模板来打印 XML。 (感谢很久以前看过的帖子。。不记得地址了)
问题:“这不起作用”:)
下面是我的示例 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/{language of chioce} 中的 DOM 操作更容易做到这一点。
-
不,克里斯...实际上我们没有使用 C#..但是我对 JAVA 不太满意,所以如果您能提出任何相同的建议,我会很乐意浏览。跨度>
-
这就是为什么我将 {language of choice} 添加到列表中的原因,因为这些天几乎每个环境都有一个 XML DOM 解析器,这就是为什么我问你为什么要限制自己使用 XSLT。跨度>
-
所以您想获得
? -
@ChrisJ:您似乎不了解 XSLT 2.0。 5 到 6 年前,人们认为使用 DOM 可能是查询/处理 XML 的众多替代方案中最糟糕的选择。