简体   繁体   English

两个带有 xslt 语言的“xsl:for-each”cicle 内的计数器项

[英]Counter item inside two "xsl:for-each" cicle with xslt language

I wish I could count the total rows of the two for-each would be possible?我希望我可以计算两个 for-each 的总行数可能吗?

The xml is: 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>

this is the xsl这是 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>

the output is :输出是:
number 123456编号 123456
0001 | 0001 | test description测试说明
0002 | 0002 | test description测试说明
number 789456编号 789456
0003 | 0003 | test description测试说明
0004 | 0004 | test description测试说明

If I use that xsl code I know that the lines are 4, but I would like the count to return me 6 I would like to know the exact number of lines.如果我使用该 xsl 代码,我知道这些行是 4,但我希望计数返回 6 我想知道确切的行数。 At the moment I use an inaccurate system and I only count the lines of the second loop.目前我使用了一个不准确的系统,我只计算第二个循环的行数。

you could add a variable in your outer loop with the current position and use it in your calculation您可以在当前位置的外循环中添加一个变量并在计算中使用它

<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 ">

Given a well-formed input such as:给定一个格式良好的输入,例如:

XML 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>

the following stylesheet:以下样式表:

XSLT 1.0 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>

will return:将返回:

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

To assign a cumulative counter to each LineRefer (while including the parent Data in the count), you can do something like:要为每个LineRefer分配一个累积计数器(同时在计数中包括父Data ),您可以执行以下操作:

XSLT 1.0 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>

to get:要得到:

Result结果

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM