0
votes

How to get number of occurrence of any child value. From below code I have child value <Name>Network B</Name> ie 'Network B'. Now How to find how many operatorstation node have this value.

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w3="http://www.w3.org">
  <xsl:output method="html" indent="yes"/>
  <xsl:variable name="allStations" select="//w3:OperatorStation"/>
  <xsl:template match="/">

    <xsl:value-of select="count($allStations[Nodes/ChildNodes/Name = 'Network B'])"/>
    //Here it return 0(zero) count always

  </xsl:template>
</xsl:stylesheet>

Input XML file

<?xml version="1.0" encoding="utf-8"?>
    <OperatorStationCollection xmlns="http://www.w3.org" >
    <OperatorStation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">        
        <Nodes>
          <ChildNodes>            
            <Name>Network A</Name>              
          </ChildNodes>          
          </Nodes>   
      </OperatorStation>      
    <OperatorStation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">        
      <Nodes>
        <ChildNodes>          
          <Name>Network B</Name>         
        </ChildNodes>        
      </Nodes>
    </OperatorStation>
    <OperatorStation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">        
      <Nodes>
         <ChildNodes>            
            <Name>Network A</Name>            
         </ChildNodes>              
      </Nodes>
    </OperatorStation>
    </OperatorStationCollection>

Desired Outout: No of occurrence of 'Network B' here its 1

1

1 Answers

0
votes

The default namespace declaration on

<OperatorStationCollection xmlns="http://www.w3.org" >

means that all its descendant elements (including Nodes, ChildNodes and Name) belong to this namespace, so you need to use the w3: prefix on those steps in the path too:

<xsl:value-of select="count($allStations[
       w3:Nodes/w3:ChildNodes/w3:Name = 'Network B'])"/>

Your current XSLT is looking for Nodes, ChildNodes and Name elements that are not in a namespace and is correctly determining that there are none.