使用xpath选择具有一组多个属性/值的元素

Hec*_*tor 31 xml xslt xpath

我有一个XML文档,我需要删除特定的数据

xml文档的结构如下: -

<a>
   <b select='yes please'>
       <c d='text1' e='text11'/>
       <c d='text2' e='text12'/>
       <c d='text3' e='text13'/>
       <c d='text4' e='text14'/>
       <c d='text5' e='text15'/>
   </b>
 </a>
<a>
   <b select='no thanks'>
       <c d='text1' e='text21'/>
       <c d='text3' e='text23'/>
       <c d='text5' e='text25'/>
   </b>
 </a>
<a>
   <b select='yes please'>
       <c d='text1' e='text31'/>
       <c d='text2' e='text32'/>
       <c d='text3' e='text33'/>
       <c d='text4' e='text34'/>
       <c d='text5' e='text35'/>
   </b>
 </a>
<a>
   <b select='no thanks'>
       <c d='text4' e='text41'/>
       <c d='text3' e='text43'/>
       <c d='text5' e='text45'/>
   </b>
 </a>
Run Code Online (Sandbox Code Playgroud)

我只需要选择那些具有d attribute ='text1'和d attribute ='text4'的/ a/b元素组,一旦我识别出这些子文档,我想获得具有d属性值的e属性的值text5'

希望这清楚

干杯

DD

Kir*_*huk 35

您可以使用此XPath:

//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e
Run Code Online (Sandbox Code Playgroud)

这将选择e='text15'e='text35'第一和第三的a/b

XSLT:

<xsl:template match="//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']">
  <xsl:value-of select="@e"/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

  • @empo,但如果它如此"微妙",@ downvoter可以解释他的意见.我不喜欢沉默的@downvoters. (5认同)

Mic*_*Kay 5

我只需要选择那些具有d attribute ='text1'和d attribute ='text4'的/ a/b元素组,一旦我识别出这些子文档,我想获得具有d属性值的e属性的值text5'

希望这清楚

是的,很清楚XPath的翻译几乎是机械的

(: those /a/b element groups :) a/b 
(: that have d attribute = 'text1' :) [c/@d='text1'] 
(: and d attribute = 'text4' :) [c/@d='text4'] 
(: and .. i want to get the value of the e attributes 
   with d attribute value 'text5' :) / c[@d='text5'] / @e
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考 - 对于那些不熟悉的人,`(:: :)是XPath 2.0评论.http://www.w3.org/TR/xpath20/#prod-xpath-Comment如果您使用的是XPath 1.0,请删除注释并使用XPath语句,即`a/b [c/@ d ='text1' ] [C/@ d = '文本4']/C [@ d = 'text5']/@ e` (2认同)