【问题标题】:xpath get top most parent for a matched node or its child nodexpath 获取匹配节点或其子节点的最高父节点
【发布时间】:2014-01-11 13:24:23
【问题描述】:

我在根目录下直接有这样的xml节点,

<online.png>
 <createdBy>admin</createdBy>
 <mimeType>image/png</mimeType> // may be here in node itself
 <primaryType>nt:file</primaryType>
 <content>
    <firstName>Vendor</firstName>
    <mimeType>image/png</mimeType>  // may be here in child 
    <city>Hyderabad</city>
 </content>
</online.png>

从这个节点,如何使用 xpath 获取顶部父节点(这里是 gif 图像节点),而不管 mimetype 位置如何,我还想首先根据其值获取节点名称,如搜索。即,

stirng xpath = select top-paren where element name has value ('image/')
           / --    "//*[contains(text(),'image/')] select top parent"

欢迎提出任何建议。

XmlNodeList nodeImages = xmlDoc.SelectNodes("//*[contains(text(),'image/')]");

返回内容节点和 gif 节点。我只想要gif节点

【问题讨论】:

  • 我在你的 xml 中搜索了 gif 并没有找到任何东西
  • 对不起,这是 png 不是 gif
  • 您要选择&lt;online.png&gt; 元素吗?

标签: c# xml xpath


【解决方案1】:

假设“top-parent”在您的示例中是指 &lt;online.png&gt;,答案非常简单:/*[//*[contains(text(), 'image')]] - 换句话说,“任何直接包含在根元素,在任意深度都包含一个包含文本“图像”的元素”。

【讨论】:

  • 谢谢您的回答,但我运行此查询时得到的结果是“根”节点,而不是 online.png 节点。
  • 也许你应该给我们包含片段的XML。
【解决方案2】:

如果您想获取二级元素(即直接放置在根目录下),其任何子元素都包含文本 image/,然后使用 xpath:

"/*/*[//*[contains(text(), 'image/')]]"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-30
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多