Xml 计算XSLT1.0中的行数

Xml 计算XSLT1.0中的行数,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我需要一些关于XSLT的帮助 我得到了一个XML文件,需要在文件中报告两次一些数据,并且需要过滤掉soem数据 在页脚上,我需要准确地报告文件中有多少行 有人能帮我吗?谢谢 以下是我的XML: <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <ns0:File xmlns:ns0="file"> <ns0:Records> <ns0:Main> <

我需要一些关于XSLT的帮助

我得到了一个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,每个type=a有2行

有人能告诉我如何才能正确获得文件中的行数吗

正如您所看到的,ns0:NumberOfRecords在这里会返回5,但实际上这个文件有6行过滤掉type=c,每个type=a有2行

而不是

<xsl:value-of select="ns0:NumberOfRecords"/>
XML和代码的其他问题:

xsl:if和xsl:choose的计算结果都不是true。因此,唯一输出的是记录的总数。这是因为上下文。 我所说的上下文是指以下内容。您的模板匹配ns0:Main元素,因此这是您在树中的位置。现在,像这样的一行:

<xsl:if test=" ns0:Factor !=0 and ns0:Type !='c' ">
或者,更改模板匹配可能更容易:

<xsl:template match="ns0:Main/ns0:Info">
您没有声明XSLT命名空间xmlns:xsl=http://www.w3.org/1999/XSL/Transformin 你的样式表 在输入XML中,有一个多余的元素会过早关闭并阻止文件格式正确 事实上我找到了这个

这将计算换行数并告诉您文件中有多少行:


很明显,你看了我的答案,因为你纠正了我指出的代码中的错误。你介意告诉我这是否解决了你的问题吗?顺便说一句,请接受这里的一个答案:谢谢你,朋友。然而,这只是简化的示例,我有一些额外的过滤器,它们基于ns0:Main下声明的变量。我需要在ns0:Footer下再次声明它们,对吗?但我不认为它们在页脚上起作用,因为每个变量都需要每个条目的ns0:Main中的数据。对于您将来的问题,在简化XML数据时,请确保只消除不会导致问题的内容。XSLT中没有过滤器。你这是什么意思?如果没有看到实际的样式表,就无法判断您的方法是否正确。或者我可以在Main中声明footer,但我如何使它出现一次??如果您是指重用变量,一般来说,变量的范围仅限于它们在代码中出现的位置。它们可以是使它们成为全局的顶级元素,也可以是内部元素xsl:template只能在这个模板中使用,或者xsl:for-each只能在这个循环中使用。我真的不知道你在说什么。页脚模板只触发一次。您只需获得一次它的输出*6。
<xsl:if test=" ns0:Factor !=0 and ns0:Type !='c' ">
<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