xsl两个for-each计数器项目

时间:2018-12-14 14:53:10

标签: xslt count

我希望我可以计算两个for-for的总行数吗?

该xml是:

<root xmlns="">
<General>
 <Data>
<Number>123456</Number>
<Date>2018-10-22</Data>
<LineRefer>0001</LineRefer>
<LineRefer>0002</LineRefer>
</Data>
<Data>
<Number>789456</Number>
<Date>2018-10-22</Data>
<LineRefer>0003</LineRefer>
<LineRefer>0004</LineRefer>
</Data>
</General>
<Services>
<Details>
<LineNumber>0001</LineNumber>
<Description>test description</Description>
</Details>
<Details>
<LineNumber>0002</LineNumber>
<Description>test description</Description>
</Details>
<Details>
<LineNumber>0003</LineNumber>
<Description>test description</Description>
</Details>
<Details>
<LineNumber>0004</LineNumber>
<Description>test description</Description>
</Details>
</root>

这是xsl

       <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" />

  <xsl:key name="services" match="Services/Details" use="LineNumber" />

  <xsl:template match="root">
<html>
<head>
  <title>HTML Document Example</title>
</head>
<body>
  <div class="page">
    <xsl:for-each select="General/Data" >
      <h1><xsl:value-of select="Number" /></h1>
      <ul>
        <xsl:for-each select="key('services', LineRefer)">
           <li><xsl:value-of select="LineNumber" /> | <xsl:value-of   select="Description" /> </li>
      <xsl:if test="(count(preceding-sibling::*) + 1) mod 40 = 0 ">
       <!--here insert the pageBreak-->
      </xsl:if>
        </xsl:for-each>
      </ul>
    </xsl:for-each>
            </div>
           </body>
        </html>
     </xsl:template>
    </xsl:stylesheet>

输出为:
数字123456
0001 |测试说明
0002 |测试说明
号码789456
0003 |测试说明
0004 |测试说明

如果我使用该xsl代码,我知道行数是4,但是我希望计数返回6 我想知道确切的行数。目前,我使用的系统不准确,我只计算了第二个循环的行数。

2 个答案:

答案 0 :(得分:0)

您可以在外循环中添加具有当前位置的变量,并在计算中使用它

<xsl:for-each select="General/Data" >
<xsl:variable name="outerPos" select="position()"/>

<xsl:for-each select="key('services', LineRefer)">
    <li><xsl:value-of select="LineNumber" /> | <xsl:value-of   select="Description" /> </li>
    <xsl:if test="(count(preceding-sibling::*) + $outerPos) mod 40 = 0 ">

答案 1 :(得分:0)

给出格式正确的输入,例如:

XML

<root xmlns="">
  <General>
    <Data>
      <Number>123456</Number>
      <Date>2018-10-22</Date>
      <LineRefer>0001</LineRefer>
      <LineRefer>0002</LineRefer>
    </Data>
    <Data>
      <Number>789456</Number>
      <Date>2018-10-22</Date>
      <LineRefer>0003</LineRefer>
      <LineRefer>0004</LineRefer>
      <LineRefer>0005</LineRefer>
    </Data>
  </General>
  <Services>
    <Details>
      <LineNumber>0001</LineNumber>
      <Description>test description</Description>
    </Details>
    <Details>
      <LineNumber>0002</LineNumber>
      <Description>test description</Description>
    </Details>
    <Details>
      <LineNumber>0003</LineNumber>
      <Description>test description</Description>
    </Details>
    <Details>
      <LineNumber>0004</LineNumber>
      <Description>test description</Description>
    </Details>
    <Details>
      <LineNumber>0005</LineNumber>
      <Description>test description</Description>
    </Details>
  </Services>
</root>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/root">
    <count-lines>
        <xsl:value-of select="count(General/Data) + count(General/Data/LineRefer)"/>
    </count-lines>
</xsl:template>

</xsl:stylesheet>

将返回:

<?xml version="1.0" encoding="utf-8"?>
<count-lines>7</count-lines>

要为每个LineRefer分配一个累积计数器(同时在计数中包括父Data),您可以执行以下操作:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/root">
    <output>
        <xsl:for-each select="General/Data">
            <xsl:variable name="i" select="position()" />
            <xsl:for-each select="LineRefer">
                <xsl:variable name="j">
                    <xsl:number count="LineRefer" level="any"/>
                </xsl:variable>
                <line-num>
                    <xsl:value-of select="$i + $j"/>
                </line-num>
            </xsl:for-each>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

获得:

结果

<?xml version="1.0" encoding="utf-8"?>
<output>
  <line-num>2</line-num>
  <line-num>3</line-num>
  <line-num>5</line-num>
  <line-num>6</line-num>
  <line-num>7</line-num>
</output>