Forum Discussion

lakshmi's avatar
lakshmi
Occasional Contributor
16 years ago

[Urgent] Getting the child count for a node

Hello

I would like to find out how to get the children count for a node
given the xpath like expression. I am using groovy as scripting lang.

The problem is that I only have the xpath expression. I am not sure
how to get node representing path and hence size or children count.

Any help over here asap is appreciated.

Thanks,
Lakshmi

6 Replies

  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    can you show the XML explain exactly what you want to count?

    regards!

    /Ole
    eviware.com
  • lakshmi's avatar
    lakshmi
    Occasional Contributor
    Hi

    Below is the structure of the response xml that i am trying to parse

    -s:envelop
    -s:body
      -AA
      -BB
        -CC
          -DD1
            @x: value1
            y : value2
            z : value3
            .
            .
          -DD2
            .
            .
            .
            .
          -DD3
              .
              .
          -DD(n)
     
        -EE1
          -FF1
          -CC
            -DD(n+1)
              @a :value4
              a  :value5
              .   
              .


    I am trying to capture the "values." of the individual node elements by looping,but am not sure how to get the children count for node given the x path like expression

    here's a sample xpath expression
    def hold = "//ns1:CC["+x+"]

    where x is a running number.

    Basically i need to get a child count for a Node but i only have the x path expression with me.Not sure how to proceed further.

    Thanks,
    Lakshmi
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi Lakshmi,

    try first getting the number of CCs with

    def cnt = holder["count(//ns1:CC)"]

    and then using the cnt in a loop!?

    regards,

    /Ole
    eviware.com
  • lakshmi's avatar
    lakshmi
    Occasional Contributor
    Hi Thanks for your reply,

    We have another problem though.
    We need to walk the tree in the right order.So can you please tell us how to get the separate counts for CC with respect to its two different parents namely BB and EE1

    Thanks,
    Lakshmi
  • omatzura's avatar
    omatzura
    Super Contributor
    Hi,

    try

    def cntBB = holder["count(//ns1:BB/ns1:CC)"]
    def cntEE1 = holder["count(//ns1:EE1/ns1:CC)"]

    ?

    /Ole
    eviware.com
  • lakshmi's avatar
    lakshmi
    Occasional Contributor
    Thanks again for the logic .It works just  fine 

    Regards,
    Lakshmi