我有这个 xml:
<mappings>
<mapping>
<name iskey="true">234</name>
<aid iskey="true">bmz</aid>
<bid iskey="true">sim</bid>
<data>GSSS</data>
</mapping>
<mapping>
<aid iskey="true">bmz</aid>
<bid iskey="true">sim</bid>
<data>TS</data>
</mapping>
<mapping>
<aid iskey="true">bmz</aid>
<account>TS</account>
</mapping>
</mappings>
I need the xpath to select the node which has node
<aid iskey='true'>bmz</aid>
and no other node containing iskey attribute.
最佳答案
在评论中反复讨论后,您似乎希望获得 mapping
。具有 aid
的节点(或与此相关的任何其他节点)具有特定属性和值但没有其他子节点具有 @iskey
的子节点属性。你这样做:
//*[aid/@iskey='true' and aid/text()='bmz'][not(*[@iskey][local-name() != 'aid'])]
谓词准确地说:有 aid
具有这些值的子节点,并且没有名称不是 aid
的子节点那会碰巧有一个 @iskey
属性。
当我运行这个简单的测试样式表时:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="//*[aid/@iskey='true' and aid/text()='bmz'][not(*[@iskey][local-name() != 'aid'])]"/>
</xsl:template>
</xsl:stylesheet>
在您的输入文档中,我得到以下返回:
<mapping>
<aid iskey="true">bmz</aid> <!-- I need this node -->
<account>TS</account>
</mapping>
UPDATE 如果您希望获得只有一个子节点带有 @iskey
的节点,你可以逃脱:
//*[count(*[@iskey]) = 1]
我将它插入到我的测试样式表中,它产生了相同的预期结果:
<mapping>
<aid iskey="true">bmz</aid> <!-- I need this node -->
<account>TS</account>
</mapping>
关于xml - 在 xpath 中需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10557994/