xml - 在 xpath 中需要帮助

标签 xml xpath

我有这个 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/

相关文章:

java - 如何将xml扩充为 fragment 类?

php - 使用 XSLT 将 XML 转换为 SQL

python - xpath - 选择父级,其中子级具有带有命名空间字典的特定属性值

python - 为命名空间中的属性设置新值

java - xpath 获取表内的行

xpath - 如何使用xdmp :estimate() in searchable XPath expression?

c# - 将 Excel XML 电子表格转换为 xlsx

c# - 为什么 .NET XML 将 xlmns 属性附加到我添加到文档的 XmlElements?我能阻止吗?

iphone - XML 作为 iPhone 中的数据库

xml - xslt 和 xpath : match directly preceding comments