xpath:选择父级和过滤后的子级 [英] xpath: select parent and filtered children

查看:33
本文介绍了xpath:选择父级和过滤后的子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 xml 块:

Given an xml block of:

<parent>
    <child id="1" />
    <child id="2" />
</parent>

如何使用 xpath 返回

How might I use xpath to return

<parent>
    <child id="1" />
</parent>

基于 id="1"(不是 firstchild)的过滤器.

Based on a filter for id="1" (not firstchild).

我不是在寻找 xslt 或 xquery 解决方案.

I am not looking for an xslt or xquery solution.

非常感谢.

推荐答案

仅使用 XPath,您无法做到.

With just XPath, you can't.

XPath 允许您从 XML 文档树中查询(选择)节点,但它不能修改树或创建新节点.因此,如果您选择原始 节点,它将有两个 子节点,并且您无法更改它.为了获得只有一个孩子的 ,您必须修改原始 以删除其另一个孩子,或者创建一个新的 .

XPath allows you to query (select) nodes from an XML document tree, but it can't modify a tree or create new nodes. So if you select the original <parent> node, it will have two <child> children, and you can't change that. In order to get a <parent> with only one child, you'd have to either modify the original <parent> to delete its other child, or create a new <parent>.

正如您所提到的,您可以使用 XSLT 做到这一点;或许多其他 XML 树构建技术.如果您告诉我们您正在构建的平台类型,我们可以推荐与您的平台最相关的平台.

You could do this with XSLT, as you alluded to; or a number of other XML tree-building technologies. If you tell us what kind of platform you're building on, we could suggest ones that are most relevant to your platform.

这篇关于xpath:选择父级和过滤后的子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆