xml 使用 xpath 选择具有一组多个属性/值的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6718335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use xpath to select elements with a set of multiple attributes/values
提问by Hector
I have an XML document that i need to strip out particular pieces of data
我有一个 XML 文档,我需要去除特定的数据片段
the xml document has a structure as follows:-
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>
i need to select only those /a/b element groups that have d attribute = 'text1' and d attribute = 'text4', once i have identified these sub documents i want to get the value of the e attributes with d attribute value 'text5'
我只需要选择那些具有 d 属性 = 'text1' 和 d 属性 = 'text4' 的 /a/b 元素组,一旦我确定了这些子文档,我想获取具有 d 属性值的 e 属性的值'文本5'
hope thats clear
希望这很清楚
Cheers
干杯
DD
DD
回答by Kirill Polishchuk
You can use this XPath:
您可以使用此 XPath:
//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e
It will select e='text15'and e='text35'of 1st and 3rd a/b
这将选择e='text15'与e='text35'第一和第三的a/b
XSLT:
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>
回答by Michael Kay
i need to select only those /a/b element groups that have d attribute = 'text1' and d attribute = 'text4', once i have identified these sub documents i want to get the value of the e attributes with d attribute value 'text5'
hope thats clear
我只需要选择那些具有 d 属性 = 'text1' 和 d 属性 = 'text4' 的 /a/b 元素组,一旦我确定了这些子文档,我想获取具有 d 属性值的 e 属性的值'文本5'
希望这很清楚
Yes, it's so clear the translation into XPath is almost mechanical
是的,很明显,转换成 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
回答by Emiliano Poggi
You can use a single template to match the required group, and then you get the value of the required attributes:
您可以使用单个模板来匹配所需的组,然后获取所需属性的值:
<xsl:template match="/*/a/b[c[@d='text1'] and c[@d='text4']]">
<xsl:value-of select="c[@d='text5']/@e"/>
</xsl:template>
assuming:
假设:
<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>
the output will be text15and text35.
输出将是text15和text35。
回答by Dimitre Novatchev
Use this single XPath expression:
使用这个单一的 XPath 表达式:
/*/a/b[c/@d='text1' and c/@d='text4']
/c[@d='text5']
/@e

