使用XPath来选择与一组多个属性的元素/值

问题描述:

我有需要去掉数据使用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> 

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

希望tha TS明确

干杯

DD

+0

+1的XPath/XSLT周日问题:) –

+7

OPS,今天是星期六... –

+0

@empo,在某些地区是星期天已经:-) –

你可以使用这个XPath:

//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e 

这将选择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> 
+0

非常好,谢谢你的时间和精力 – Hector

+0

@ user423199,不客气! –

+0

@ user423199,您可以标记为答案或upvote适当的答案。 –

您可以使用一个模板来匹配所需的组,然后你需要的属性的值:

<xsl:template match="/*/a/b[c[@d='text1'] and c[@d='text4']]"> 
    <xsl:value-of select="c[@d='text5']/@e"/> 
</xsl:template> 

假设:

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

输出将text15text35

+0

谢谢你打扰我的问题,这让我疯狂!谢谢你花费时间 – Hector

+0

@欢迎使用,这里的“棘手”部分是匹配一个元素,我们至少有两个具有不同属性值的子元素的实例,为了清晰起见,需要使用_and_和一个嵌套谓词 –

我只需要选择具有d属性='text1'和d属性='text4'的那些/ a/b元素组,一旦我确定了这些子文档,我想要得到e与d的属性值 'text5'

希望这就是clear属性

是的,它是如此清晰翻译成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 
+1

仅供参考,对于那些不熟悉的人来说,'(::)'是XPath 2.0的评论。http://www.w3.org/TR/xpath20/#prod-xpath-Comment如果您使用的是XPath 1.0,请删除注释只需使用XPath语句即'a/b [c/@ d ='text1'] [c/@ d ='text4']/c [@ d ='text5']/@ e' –

使用此XPath表达式

/*/a/b[c/@d='text1' and c/@d='text4'] 
     /c[@d='text5'] 
      /@e