使用XPath在命名空间中选择元素 [英] Select Element in a Namespace with XPath

查看:98
本文介绍了使用XPath在命名空间中选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在具有给定名称空间(前缀)的文档中选择最顶层的元素.

I want to select the topmost element in a document that has a given namespace (prefix).

更具体地说:我有一些XML文档,它们以/html/body(在XHTML名称空间中)开头,或者以一个特定名称空间中的几个元素之一开头.我实际上想剥离/html/body并只返回正文内容或整个根命名空间元素.

More specifically: I have XML documents that either start with /html/body (in the XHTML namespace) or with one of several elements in a particular namespace. I effectively want to strip out /html/body and just return the body contents OR the entire root namespaced element.

推荐答案

在XPath 2.0和XQuery 1.0中,您可以使用

In XPath 2.0 and XQuery 1.0 you can test against the namespace prefix using the in-scope-prefixes() function in a predicate. e.g.

//*[in-scope-prefixes(.)='html']

如果您不能使用v2,则可以在XPath 1.0中使用 namespace-uri ()函数来测试名称空间本身. 例如

If you cant use v2, in XPath 1.0 you can use the namespace-uri() function to test against the namespace itself. e.g.

//*[namespace-uri()='http://www.w3c.org/1999/xhtml']

这篇关于使用XPath在命名空间中选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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