首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用xpath选择具有一组多个属性/值的元素

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

Stack Overflow用户
提问于 2011-07-16 23:26:16
回答 4查看 67.5K关注 0票数 33

我有一个需要剥离特定数据片段的XML文档

xml文档的结构如下:

代码语言:javascript
复制
<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 attribute = 'text1‘和d attribute = 'text4’的/a/b元素组,一旦我确定了这些子文档,我希望获得具有d属性值'text5‘的e属性值。

希望这是清楚的

干杯

DD

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-16 23:44:02

您可以使用此XPath:

代码语言:javascript
复制
//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e

它将选择第一个和第三个a/be='text15'e='text35'

XSLT:

代码语言:javascript
复制
<xsl:template match="//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']">
  <xsl:value-of select="@e"/>
</xsl:template>
票数 35
EN

Stack Overflow用户

发布于 2011-07-17 01:10:00

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

希望这是清楚的

是的,很明显,转换成XPath几乎是机械的

代码语言:javascript
复制
(: 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
票数 5
EN

Stack Overflow用户

发布于 2011-07-16 23:46:57

您可以使用单个模板来匹配所需的组,然后您可以获得所需属性的值:

代码语言:javascript
复制
<xsl:template match="/*/a/b[c[@d='text1'] and c[@d='text4']]">
    <xsl:value-of select="c[@d='text5']/@e"/>
</xsl:template>

假设:

代码语言:javascript
复制
<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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6718335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档