How to count nodes in XSLT?

Now I have come to the last step of my XSL stylesheet, in which I need to print a number representing the total number of nodes added. In fact, it seems to me that this is not so straightforward:

First of all, I am not calculating nodes from the source XML document, I am going to count some nodes from the resulting XML (the source XML document may be empty).

Secondly, I do not consider all nodes.

For example, here is a snippet of my XSLT code:

<xsl:template name="Loop2000A">
  <Loop2000A>
<HL>
  <HL01>
    <xsl:value-of select="'1'"/>
  </HL01>
  <HL03>
    <xsl:value-of select="'20'"/>
  </HL03>
</HL>
 <xsl:if test="$recbat//provider_taxonomy_qual !='' ">
<PRV>
 <PRV01>
   <xsl:value-of select="'BI'"/>
 </PRV01>
     <PRV02>
   <xsl:value-of select="$recbat//provider_taxonomy_qual"/>
 </PRV02>
<PRV03>
   <xsl:value-of select="$recbat//provider_taxonomy"/>
</PRV03>
   </PRV>
  </xsl:if>
<xsl:call-template name="Loop2010AA"/>

, , $recbat .. . , , (, "Loop2010AA" , ), , 3 , 3, , , "if" , , .

, , , , XML. , , , , ?

, . , , XML-, , xml, TRUE- .

+3
1

, :

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

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:variable name="vrtfPass1">
   <top>
    <xsl:apply-templates select="*"/>
    <xsl:apply-templates select="*"/>
   </top>
  </xsl:variable>

  <xsl:variable name="vPass2" select="ext:node-set($vrtfPass1)"/>

  <xsl:value-of select="count($vPass2/*/*/*)"/>
 </xsl:template>
</xsl:stylesheet>

, XML-:

<nums>
 <num>1</num>
 <num>2</num>
 <num>3</num>
 <num>4</num>
 <num>5</num>
</nums>

:

10

XSLT 1.0 () xxx:node-set() . XSLT 2.0 , RTT- .

+3
source

Source: https://habr.com/ru/post/1789068/


All Articles