XPath 将元素中的所有文本作为一个值,删除换行符 [英] XPath to get all text in element as one value, removing line breaks

查看:33
本文介绍了XPath 将元素中的所有文本作为一个值,删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取节点中的所有文本以获取以下集合并作为一个值(不是多个节点)返回.

I am trying to get all the text in a node for a following set and returning as one value (not multiple nodes).

<p>
   "I love eating out."
   <br>
   <br>
   "This is my favorite restaurant."
   <br>
   "I will definitely be back"
</p>

我正在使用 '/p' 并获得所有结果,但它返回时带有换行符.同样尝试 '/p/text()' 会导致将每个标签之间的每个文本作为单独的返回值.理想的回报是--

I am using '/p' and get all the results but it returns with line breaks. Also trying '/p/text()' results in getting each text between each tag as a separate returned value. The ideal return would be --

"I love eating out. This is my favorite restaurant. I will definitely be back"

我已尝试搜索其他问题,但找不到与此类似的内容.请注意,在当前环境中,我仅限于使用 XPath 查询,并且无法解析或设置任何 HTML 预解析.具体来说,我在 Google Docs 中使用 importXML 函数.

I've tried searching other questions but couldn't find something as close. Please not that in the current environment I am restricted to only use an XPath Query and cannot parse after or setup any HTML pre-parsing. Specifically I'm using the importXML function inside of Google Docs.

推荐答案

使用:

normalize-space(/)

当计算此 XPath 表达式时,首先生成文档节点 (/) 的字符串值,并将其作为参数提供给标准 XPath 函数 normalize-space().

When this XPath expression is evaluated, the string value of the document node (/) is first produced and this is provided as argument to the standard XPath function normalize-space().

根据定义,normalize-space() 返回其参数,消除了前导和尾随相邻空白字符,并且任何中间的此类相邻空白字符组 - 替换为单个空格字符.

By definition, normalize-space() returns its argument with the leading and trailing adjacent whitespace characters eliminated, and any interim such group of adjacent whitespace characters -- replaced by a single space character.

上述 XPath 表达式的求值结果:

我喜欢外出就餐."这是我最喜欢的餐厅."我一定会回来的"

"I love eating out." "This is my favorite restaurant." "I will definitely be back"

为了消除引号,我们另外使用 translate()函数:

normalize-space(translate(/,'&quot;', ''))

计算这个表达式的结果是:

I love eating out. This is my favorite restaurant. I will definitely be back

最后,为了让这个结果用引号括起来,我们使用 concat() 函数:

concat('&quot;',
       normalize-space(translate(/,'&quot;', '')),
       '&quot;'
       )

对这个 XPath 表达式的评估产生了想要的结果:

"I love eating out. This is my favorite restaurant. I will definitely be back"

基于 XSLT 的验证:

<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:value-of select=
   "concat('&quot;',
           normalize-space(translate(/,'&quot;', '')),
           '&quot;'
           )"/>
 </xsl:template>
</xsl:stylesheet>

在提供的 XML 文档上应用此转换时(已更正为格式正确):

When this transformation is applied on the provided XML document (corrected to be made well-formed):

<p>
       "I love eating out."
       <br />
       <br />
       "This is my favorite restaurant."
       <br />
       "I will definitely be back"
</p>

计算 XPath 表达式并将计算结果复制到输出:

"I love eating out. This is my favorite restaurant. I will definitely be back"

这篇关于XPath 将元素中的所有文本作为一个值,删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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