如何在XPath中进行多路连接?

时间:2016-05-23 18:20:28

标签: xml xpath

鉴于此XML:

<?xml version="1.0" encoding="UTF-8"?>
<r> 
  <Rk> 
    <Req>111</Req>  
    <RkNumber>123456</RkNumber>  
    <RkName>John</RkName> 
  </Rk>  
  <Rk> 
    <Req>111</Req>  
    <RkNumber>9876</RkNumber>  
    <RkName>Joe</RkName> 
  </Rk>  
  <Covg> 
    <Req>111</Req>  
    <RkNumber>123456</RkNumber>  
    <CvgNumber>123</CvgNumber> 
  </Covg>  
  <Covg> 
    <Req>111</Req>  
    <RkNumber>9876</RkNumber>  
    <CvgNumber>321</CvgNumber> 
  </Covg>  
  <Comp> 
    <Req>111</Req>  
    <CvgNumber>123</CvgNumber>  
    <CompTyp>ABC</CompTyp> 
  </Comp>  
  <Comp> 
    <Req>111</Req>  
    <CvgNumber>321</CvgNumber>  
    <CompTyp>DEF</CompTyp> 
  </Comp> 
</r>

我想选择RkName =&#39; ABC&#39;的CompTyp。 我试过这个XPath,

//Rk[RkNumber = //Covg/RkNumber and //Covg/CvgNumber = //Comp/CvgNumber and //Comp[CompTyp = 'ABC']]/RkName

但它返回&#34; John&#34;和&#34;乔&#34;。我只希望有&#34; John&#34;返回。

1 个答案:

答案 0 :(得分:1)

使用此XPath,

//Rk[RkNumber=//Covg[CvgNumber=//Comp[CompTyp='ABC']/CvgNumber]/RkNumber]/RkName

通过RkName选择与Covg相关联的RkNumberComp通过CvgNumber通过CompCompTyp相关联使用ABC <RkName>John</RkName>

 @Override
public boolean onLongClick(View view) {
    if (!mMultiSelector.isSelectable()) {
        ((AppCompatActivity) getActivity()).startSupportActionMode(mActionModeCallback); // (2)
        mMultiSelector.setSelectable(true);
        mMultiSelector.setSelected(MyViewHolder.this, true);
        return true;
    }
    return false;
}

按要求。