值与XML元素匹配,然后返回所有兄弟属性值

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

本文关键字:兄弟 属性 返回 然后 元素 XML 值与      更新时间:2023-09-26

我试图获得所有属性值,如果属性值在我的XML文件。

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

但是它返回的是节点的顶部。
当输入的值是匹配的,那么它的返回值在所有属性值之上。
但是我想要所有的兄弟属性值。
这是我的ml格式:-

<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>

当前输出为:-

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

预期输出:-

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

如何获得所有属性值,请解决此查询。
谢谢。

稍微改变一下xapht就可以了。
/ancestor::product_id/descendant-or-self::*代替/preceding-sibling::*

解释你做了什么:

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

您正在寻找具有属性值中给定值的dist_region。例如,元素<dist_region value="4909"/>
该元素的下一步是'/preceding-sibling::*/@*它们是所有元素的所有属性值,按文档顺序排列在当前元素之前,处于同一级别。这将导致您看到的输出。

你应该怎么做:
因为你的语句预期输出:-
似乎您喜欢获取当前dist_region所属的product_id的所有属性。因此使用:

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

因为:ancestor::product_id查找product_id 以上形式的当前元素。和下一步descendant-or-self::*/@*找到任何深度的任何子属性。

一些附加命令:
我不知道你为什么用//*[local-name() = "dist_region" and ...]
//dist_region[...]/也应该这样做。

输出:

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