0

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>
1
  • Note that the error message indicates you are using XSLT 1.0 (in later versions you would get no failure, just the answer "true", because count() can be applied to any value and if you apply it to a boolean the answer is 1. Since you are new to XSLT, you should be aware that although XSLT 1.0 is about 23 years old and has been long eclipsed by more powerful versions 2.0 and 3.0, there are still many XSLT processors that haven't been upgraded to later versions. In SO questions you should always indicate which version you are using. Commented Jan 28, 2023 at 23:36

1 Answer 1

1

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">
Sign up to request clarification or add additional context in comments.

1 Comment

Or simply: <xsl:if test="/List/Records/Record[StatusCode='N']">. –

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.