Home > Blockchain >  How to get the node counts in XML using XSLT - count
How to get the node counts in XML using XSLT - count

Time:01-29

I am new to XML operation, I am using XSLT file in that I need to get the count of the node only in case when StatusCode ='N', Kindly assist me , where I am doing mistake here.

This is my XML:

<List>
  <LockingTS>aj8s=</AccountLockingTS>
  <Id>10113</AccountId>
  <Records>
    <Record>
      <Direction>123876871</Direction>
      <EntryDate>2023-01-28T00:00:00</EntryDate>
      <PaymentMethod>1EFT</PaymentMethod>
      <StatusCode>N</StatusCode>
      <CurrencyCode>USD</CurrencyCode>
    </Record>
  </Records>
</List>

This is what I have tried : "count(//List/Records/Record/StatusCode='N')>0"

<xsl:if test="count(//List/Records/Record/StatusCode='N')&gt;0">

But its showing me an error message :-

 Argument 1 of function 'count()' cannot be converted to a node-set. --&gt;count(//List...StatusCode='N')&lt;-- &gt;0</error>

CodePudding user response:

Let's first fix that XML; the LockingTS and Id tags are not closed correctly.

<List>
  <LockingTS>aj8s=</LockingTS>
  <Id>10113</Id>
  <Records>
    <Record>
      <Direction>123876871</Direction>
      <EntryDate>2023-01-28T00:00:00</EntryDate>
      <PaymentMethod>1EFT</PaymentMethod>
      <StatusCode>N</StatusCode>
      <CurrencyCode>USD</CurrencyCode>
    </Record>
  </Records>
</List>

About that count statement.
The definition of the filter is not correct; it must be between square brackets.

//List/Records/Record[StatusCode='N']

The xsl:if then looks like below.

<xsl:if test="count(//List/Records/Record[StatusCode='N']) &gt; 0">
  • Related