0

I have a document that appears like this:

<Subdocument Label="Subdocument">
 <Lines Label="Lines">
   <Line>
    <ITEM>AAA</ITEM>
   </Line>
 </Lines>
</Subdocument>

<Subdocument Label="Subdocument">
 <Lines Label="Lines">
   <Line>
    <ITEM>AAA</ITEM>
   </Line>
 </Lines>
</Subdocument>

<Subdocument Label="Subdocument">
 <Lines Label="Lines">
   <Line>
    <ITEM>BBB</ITEM>
   </Line>
 </Lines>
</Subdocument>

<Subdocument Label="Subdocument">
 <Lines Label="Lines">
   <Line>
    <ITEM>BBB</ITEM>
   </Line>
 </Lines>
</Subdocument>

Is there a way to write an XPath 1.0 using the count syntax of how many times a repeating value appears in the document? The expected result would be that AAA appeared 2 times.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Marc
  • 9
  • 1

2 Answers2

0

Following XPath expression would do the job:

count(.//*[text()='AAA'])

But please note, that XML in your example is not syntactically correct ("Subdocument" elements should be wrapped in some root element).

Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27
  • I should have tried to be more articulate with the problem. Sorry! The values of the node are unknown to me until a transaction occurs. The challenge has been how many times the node appears with the same values repeatedly. I am starting to think that there is no XPATH 1.0 to address this problem as a single line syntax. – Marc Jan 10 '21 at 02:28
0

XPath to select all text nodes in an XML document that...

  • exactly equal "AAA":

    //text()[.="AAA"]
    
  • exactly equal "AAA", ignoring leading and trailing whitespace:

    //text()[normalize-space()="AAA"])
    
  • have "AAA" as a substring:

    //text()[contains(.,"AAA")]
    

Any of the above can be wrapped in count() to return the number of nodes meeting stated condition.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I should have tried to be more articulate with the problem. Sorry! The values of the node are unknown to me until a transaction occurs. The challenge has been how many times the node appears with the same values repeatedly. I am starting to think that there is no XPATH 1.0 to address this problem as a single line syntax. – Marc Jan 10 '21 at 02:28
  • Your requirements are (again) lacking. Do you mean to constrain `ITEM` to only have a single value? If not, are you asking for the maximum number of `ITEM` elements that have the same value? The minimum number? As you can see, you've ***still*** not been clear in expressing what you want. – kjhughes Jan 10 '21 at 02:51
  • "Are you asking for the maximum number of ITEM elements that have the same value?" that is what I am looking for! – Marc Jan 10 '21 at 04:55