2014-02-19 84 views
0

我需要一些关于XSLT的帮助。计算XSLT1.0中的行数

我得到一个XML文件,我需要在文件中报告一些数据两次,soem数据需要过滤掉。

并在页脚上我需要准确报告文件中有多少行。

是否有人可以帮助我在这里感谢

这里是我的XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<ns0:File xmlns:ns0="file"> 

    <ns0:Records> 
    <ns0:Main> 
     <ns0:Info> 
     <ns0:InternalCode>1</ns0:InternalCode> 
     <ns0:Name>test1</ns0:Name> 
     <ns0:Factor>2.000000000000</ns0:Factor> 
     <ns0:Type>a</ns0:Type> 
     </ns0:Info> 
    </ns0:Main> 
<ns0:Main> 
     <ns0:Info> 
     <ns0:InternalCode>2</ns0:InternalCode> 
     <ns0:Name>test2</ns0:Name> 
     <ns0:Factor>10.000000000000</ns0:Factor> 
     <ns0:Type>c</ns0:Type> 
     </ns0:Info> 
    </ns0:Main> 
<ns0:Main> 
     <ns0:Info> 
     <ns0:InternalCode>3</ns0:InternalCode> 
     <ns0:Name>test3</ns0:Name> 
     <ns0:Factor>13.000000000000</ns0:Factor> 
     <ns0:Type>b</ns0:Type> 
     </ns0:Info> 
    </ns0:Main> 
<ns0:Main> 
     <ns0:Info> 
     <ns0:InternalCode>4</ns0:InternalCode> 
     <ns0:Name>test4</ns0:Name> 
     <ns0:Factor>1.000000000000</ns0:Factor> 
     <ns0:Type>a</ns0:Type> 
     </ns0:Info> 
    </ns0:Main> 
<ns0:Main> 
     <ns0:Info> 
     <ns0:InternalCode>5</ns0:InternalCode> 
     <ns0:Name>test5</ns0:Name> 
     <ns0:Factor>1.000000000000</ns0:Factor> 
     <ns0:Type>f</ns0:Type> 
     </ns0:Info> 
    </ns0:Main> 
    </ns0:Records> 

    <ns0:Footer> 
    <ns0:Time>10:54:40</ns0:Time> 
    <ns0:NumberOfRecords>5</ns0:NumberOfRecords> 
    </ns0:Footer> 

</ns0:File> 

这里是我的XSLT:

<?xml version="1.0" encoding="iso-8859-1"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="file"> 

     <xsl:output method="text" 
     encoding="ASCII"/> 

     <xsl:template match="ns0:Main"> 

<xsl:variable name="substractone"> 
     <xsl:value-of select="ns0:Info/ns0:Factor-1"/> 
    </xsl:variable> 

     <xsl:if test=" ns0:Factor !=0 and ns0:Type !='c' and $substractone !=0 "> 


      <xsl:choose> 
      <xsl:when test="ns0:Type = 'a'"> 

       <xsl:value-of select="ns0:InternalCode"/> 
       <xsl:text>,</xsl:text> 
       <xsl:value-of select="ns0:Name"/> 
       <xsl:text>,</xsl:text> 
       <xsl:value-of select="ns0:Factor"/> 
         <xsl:text> 
    </xsl:text> 
    <!-- repeat in a new line --> 
       <xsl:value-of select="ns0:InternalCode"/> 

       <xsl:text>,</xsl:text> 
       <xsl:value-of select="ns0:Name"/> 
       <xsl:text>,</xsl:text> 
       <xsl:value-of select="ns0:Factor"/> 
         <xsl:text> 
    </xsl:text> 

      </xsl:when> 

      <xsl:otherwise> 


       <xsl:value-of select="ns0:InternalCode"/> 

       <xsl:text>,</xsl:text> 
       <xsl:value-of select="ns0:Name"/> 
       <xsl:text>,</xsl:text> 
       <xsl:value-of select="ns0:Factor"/> 
         <xsl:text> 
    </xsl:text> 
      </xsl:otherwise> 
      </xsl:choose> 


     </xsl:if> 

     </xsl:template> 

     <xsl:template match="ns0:Footer"> 
     <!--Footer row--> 
     <xsl:text> 
    </xsl:text> 

     <xsl:text>*</xsl:text> 
     <xsl:value-of select="ns0:NumberOfRecords"/> 



     <!--record total--> 


     <xsl:apply-templates/> 

     </xsl:template> 
     <xsl:template match="text()"/> 

    </xsl:stylesheet> 

正如你可以看到NS0:NumberOfRecords会在这里返回5,但实际上这个文件有4行(过滤出type = c,每行为= 2行)

有人可以告诉我如何才能正确地获取文件中的行数?

+0

你明明看着我的答案(因为你在你的代码,我指出纠正错误)。你介意告诉我它是否解决了你的问题?顺便说一下,请接受这里的答案之一:http://stackoverflow.com/questions/12743828/how-to-do-square-root-in-xslt-1-0 –

回答

0

正如你可以看到NS0:NumberOfRecords将返回5在这里,但实际上该文件得到了6行(过滤掉类型= c和2行每类= A)

代替

<xsl:value-of select="ns0:NumberOfRecords"/> 

<xsl:value-of select="count(//ns0:Type[. != 'c']) + count(//ns0:Type[. = 'a'])"/> 

然后,将得到以下的输出:

*6 

与XML和代码的其他问题:

  • 没有你xsl:ifxsl:choose的计算结果为 “真”。因此,唯一输出的是记录总数。这是因为上下文

上下文我的意思是以下。您的模板匹配ns0:Main元素,因此这是树中“您的”位置。如今,像这样的一行:

<xsl:if test=" ns0:Factor !=0 and ns0:Type !='c' "> 

实际上看起来的ns0:Factorns0:Type元素是ns0:Main元素的直接孩子。但是,如你所知,没有这样的元素。相反,ns0:Factorns0:Typens0:Info的子女。你必须考虑到这样一个事实:

<xsl:if test=" ns0:Info/ns0:Factor !=0 and ns0:Info/ns0:Type !='c' "> 

或者,也许它可能是更容易更改模板匹配:如果你做任何的这些变化

<xsl:template match="ns0:Main/ns0:Info"> 

,输出为:

1,test1,1.000000000000 
1,test1,1.000000000000 
3,test3,13.000000000000 
4,test4,1.000000000000 
4,test4,1.000000000000 
5,test5,1.000000000000 

*6 
  • 你没有声明XSLT命名空间(xmlns:xsl="http://www.w3.org/1999/XSL/Transform")在你的样式表中
  • 在您输入XML,还有盈余</ns0:Records>元素过早地关闭,防止文件被合式
+0

非常感谢大副。然而这只是简单的例子,我有一些额外的过滤器基于在ns0:Main下声明的变量。我需要再次在ns0:Footer下再次声明它们。但我不认为重新分拆他们将在页脚上工作,因为每个变量都需要ns0中的数据:Main为每个条目 – ken

+0

对于将来的问题,在简化XML数据时,确保只消除对你的问题没有贡献。在XSLT中没有这样的“过滤器”。你是什​​么意思?没有看到实际的样式表,不可能说你的方法是否“正确”。 –

+0

或者也许我可以宣布footerwithin主,但如何cna我只是让它出现一次? – ken