2011-12-12 59 views
2

这是我正在尝试实现的: 我有一个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> 
+0

XSLT的任何原因?正如你发现的那样,XSLT不知道CSV,这使得CSV成为绝对的平衡器。所以通过C#/ Java/{chioce语言}中的DOM操作来实现这一点会更容易。 –

+0

没有克里斯......其实我们并没有使用C#..但是我在JAVA中并没有那么高兴,所以如果你能提供相同的指针,我会很乐意浏览。 – Kapil

+0

这就是为什么我在列表中添加了{语言的选择},因为几乎每个环境都有一个XML DOM解析器,这就是为什么我问你为什么只限于XSLT。 –

回答

2

你的问题是,因为你有2个模板,都匹配 “/” XML文档的根,以漏斗正确的数据你必须确保优先级设置在你想要执行的模板上!由于您使用的是命名模板,因此您可以简单地从getItems模板中删除match属性!

有了这个小改变它的工作原理,除非你有不同的输出所需?

<?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="*|/" priority="999"> 
       <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> 

问候, 山姆

+0

感谢Sam ..它完美地工作。 – Kapil

+0

@Vivek,TreeMonkey:为什么不真的使用XSLT 2.0作为它的目标语言?使用它作为纯XSLT 1.0是一个耻辱。 –

+0

@DimitreNovatchev我只知道xslt 1.0在.net环境中工作,所以很快就会发生这种改变的可能性很小,我从你的回答中看到,为了我的目的,它可能非常方便! – Treemonkey

3

问得好,+1。

请注意,您正在使用XSLT 2.0,但完全没有使用任何XSLT 2.0特定功能。

下面是一个简短和简单(单个模板,没有递归,没有xsl:call-template,没有xsl:choosexsl:whenxsl:otherwiseXSLT 2.0溶液

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pErrorPos" as="xs:string" select="'1,4'"/> 

<xsl:variable name="vDeletePos" as="xs:integer*" select= 
" for $s in tokenize($pErrorPos, ',') 
    return xs:integer($s) 
"/> 

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

<xsl:template match="Item[position() = $vDeletePos]"/> 
</xsl:stylesheet> 

当施加上提供的XML文档

<Items> 
    <Item IItemID=""/> 
    <Item ItemID="100-8754"/> 
    <Item ItemID="206-4141"/> 
    <Item ItemID=""/> 
</Items> 

的希望,正确的结果产生

<Items> 
    <Item ItemID="100-8754"/> 
    <Item ItemID="206-4141"/> 
</Items> 

说明

  1. 重写Identity rule

  2. 使用标准的XPath函数tokenize()

+0

+1正确答案和xslt 2.0使用! – Treemonkey

+0

@Treemonkey:不客气。 –