I wish I could count the total rows of the two for-each would be possible?
The xml is:
<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: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
0001 | test description
0002 | test description
number 789456
0003 | test description
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. 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
<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
<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:
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.