简体   繁体   English

值与xml元素匹配,然后返回所有同级属性值。

[英]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. 我试图获得所有属性值,如果我的xml文件中的属性值。

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:- 这是我的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>

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. 稍微更改xapht即可。
Replace /preceding-sibling::* with /ancestor::product_id/descendant-or-self::* . /ancestor::product_id/descendant-or-self::*替换/preceding-sibling::* /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. 您正在寻找属性值中具有给定值的dist_region This would be for example the element <dist_region value="4909"/> . 例如,这将是元素<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. 该元素的下一步是'/preceding-sibling::*/@*它们是所有元素的所有属性值,按文档顺序排列,位于同一级别上的当前元素之前。 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. 似乎您想获取当前dist_region所属的product_id的所有属性。 Therefore use: 因此使用:

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

Because: ancestor::product_id find product_id above form current element. 因为: ancestor::product_id在当前元素上方找到product_id。 and next step descendant-or-self::*/@* find any attribute in any child in any deep. 下一步, descendant-or-self::*/@*在任何子对象中找到任何属性。
That's it. 而已。

Somme additional commands: Somme其他命令:
I do not know why your are using //*[local-name() = "dist_region" and ...] 我不知道您为什么使用//*[local-name() = "dist_region" and ...]
//dist_region[...]/ should do the same. //dist_region[...]/应该做的一样。

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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