我有一个需要剥离特定数据片段的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 attribute = 'text1‘和d attribute = 'text4’的/a/b元素组,一旦我确定了这些子文档,我希望获得具有d属性值'text5‘的e属性值。
希望这是清楚的
干杯
DD
发布于 2011-07-16 23:44:02
您可以使用此XPath:
//a[b/c/@d = 'text1' and b/c/@d = 'text4']/b/c[@d = 'text5']/@e它将选择第一个和第三个a/b的e='text15'和e='text35'
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>发布于 2011-07-17 01:10:00
我只需要选择那些具有d attribute = 'text1‘和d attribute = 'text4’的/a/b元素组,一旦我确定了这些子文档,我希望获得具有d属性值'text5‘的e属性值。
希望这是清楚的
是的,很明显,转换成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发布于 2011-07-16 23:46:57
您可以使用单个模板来匹配所需的组,然后您可以获得所需属性的值:
<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>输出将是text15和text35。
https://stackoverflow.com/questions/6718335
复制相似问题