简体   繁体   中英

value is match with xml element then return all there sibling attribute value.

i am try to get all attribute value if attribute value in my xml file.

i am try this  xpath:-
var xPath = '//*[local-name() = "dist_region" and ' +
                                        ' contains(concat(@value, ","), "' + array_top[i] + ',")]' + 
                                        '/preceding-sibling::*/@*';

but its return me top of node .
when entered value is match then its return there above all attribute value.
but i want to all there sibling attribute value.
this is my ml format:-

<products>
  <product_id value="1">
    <tab_id value="351">
      <tab_name value="test1"/>
      <region_timezone value="1"/>
      <registrationstatus value="2"/>
      <eventstatus value="2"/>
      <dist_activity value="4"/>
      <dist_activity value="10066"/>
      <dist_activity value="10070"/>
      <dist_region value="4909"/>
      <dist_region value="4902"/>
      <dist_region value="4905"/>
      <dist_value value="55"/>
      <dist_value value="342"/>
      <dist_value value="86"/>
   </tab_id>
 </product_id>
<product_id value="2">
    <tab_id value="351">
      <tab_name value="test1"/>
      <region_timezone value="1"/>
      <registrationstatus value="2"/>
      <eventstatus value="2"/>
      <dist_activity value="4"/>
      <dist_activity value="10066"/>
      <dist_activity value="10070"/>
      <dist_region value="4912"/>
      <dist_region value="4908"/>
      <dist_region value="4901"/>
      <dist_value value="55"/>
      <dist_value value="342"/>
      <dist_value value="86"/>
    </tab_id>
  </product_id>
</products>

Present output is:-

test1,1,2,2,4,10066,10070

expected Output:-

1,351,test1,1,2,2,4,10066,10070,4909,4902,4905,55,342,86

how can i get all attribute value please solve this query.
thanks.

Slightly changing your xapht will do.
Replace /preceding-sibling::* with /ancestor::product_id/descendant-or-self::* .

Explanation what you did:

'//*[local-name() = "dist_region" and  contains(concat(@value, ","), "' + array_top[i] + ',")]'

You are looking for a dist_region with a given value in attribute value. This would be for example the element <dist_region value="4909"/> .
Next step for this element is '/preceding-sibling::*/@* Which are all attribute values from all element are in document order before the current element on the same level. Which leads to the output you see.

What you should do:
Because of your statement expected Output:-
It seems you like to get all attributes of the product_id where the current dist_region belongs to. Therefore use:

ancestor::product_id/descendant-or-self::*/@*

Because: ancestor::product_id find product_id above form current element. and next step descendant-or-self::*/@* find any attribute in any child in any deep.
That's it.

Somme additional commands:
I do not know why your are using //*[local-name() = "dist_region" and ...]
//dist_region[...]/ should do the same.

Output:

 1 351 test1 1 2 2 4 10066 10070 4909 4902 4905 55 342 86

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM