如何在标记xml外部提取文本 [英] how to extract text outside tags xml

查看:56
本文介绍了如何在标记xml外部提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取标签外的文本.例如,

I want to extract text outside tags. For example,

<body>
    This is an exmaple
    <p>
        blablabla
    </p>
    <references>
        refer 1
        refer 2
    </references>
</body>

我想仅在其他标签(p或引用)中没有文本的情况下获取文本"This is a example".我尝试了几种方法,但是没有用. Any1可以提供帮助吗?非常感谢.

I want to get the text "This is an example" only without text in other tags (p or reference). I tried several methods but does not work. Any1 can help? Big thanks.

推荐答案

您必须考虑标记内的文本,例如节点.使用测试节点text()检索文本节点.例子.鉴于:

You must think a text inside a tag like a node. A text node is retrieved using the test node text(). Example. Given:

<body>
    This is an exmaple
    <p>
    blablabla
    <\p>
    <references>
        refer 1
        refer 2
    <\references>
    another example
<\body>

XPath:

"/body/text()"

将检索body的所有子文本节点,例如"This is a exmaple"和"another example",而

Will retrieve all children text nodes of body, like "This is an exmaple" and "another example", while:

"/body/text()[1]"

将仅检索第一个,这是一个赞".如果需要所有后代文本节点,则可以使用:

will retrieve only the first one, "This is an exmaple". If you want all the descendant text nodes you can use:

"/body//text()"

或者,您希望第一个p内的所有文本节点:

or, you want all the text nodes inside first p:

"/body/p[1]//text()"

这篇关于如何在标记xml外部提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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