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

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

问题描述



 <$ c我试图获取节点中的所有文本以获得以下集合并返回为一个值(不是多个节点)。 $ C>< p为H. 
我喜欢外出用餐。
< br>
< br>
这是我最喜欢的餐厅。
< br>
我一定会回来的
< / p>

我使用'/ p'并获取所有结果,但它以换行符返回。同样尝试使用'/ p / text()'会导致每个标记之间的每个文本都作为一个单独的返回值。理想的回报是 -

 我喜欢外出用餐,这是我最喜欢的餐厅,我一定会回来

我试过寻找其他问题,但找不到近似的东西。请不要在当前环境中限制只使用XPath查询,并且无法解析或设置任何HTML预解析。具体而言,我在Google文档中使用了importXML函数。

解决方案

使用

  normalize-space(/)

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



根据定义, normalize-space()
$ b>返回它的参数,消除了前后相邻的空白字符,以及任何临时这样的相邻空白字符组 - 由一个空格字符取代。

对上述XPath表达式的评估结果如下:

我喜欢外出吃饭。 这是我最喜欢的餐厅。 我一定会回来的



为了消除引号,我们另外使用 translate() 功能

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

评估此表达式的结果是

 我喜欢外出就餐。这是我最喜欢的餐厅。我肯定会回来

最后,为了让这个结果包裹在引号中,我们使用 concat() 函数

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

此XPath表达式的评估产生了精确的结果

 我喜欢外出用餐,这是我最喜欢的餐厅,我一定会回来


$ b $基于XSLT的验证:

 < xsl:stylesheet version =1.0 
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output omit-xml-declaration =yesindent =yes/>

< xsl:template match =/>
< xsl:value-of select =
concat('& quot;',
normalize-space(translate(/,'& quot;','')) ,
'& quot;'
)/>
< / xsl:template>
< / xsl:stylesheet>

将此转换应用于提供的XML文档(更正为)

 < p> 
我喜欢外出用餐。
< br />
< br />
这是我最喜欢的餐厅。
< br />
我一定会回来的
< / p>

计算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>

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"

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.

解决方案

Use:

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().

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.

The evaluation of the above XPath expression results in:

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

To eliminate the quotes, we additionally use the translate() function:

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

The result of evaluating this expression is:

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

Finally, to have this result wrapped in quotes itself, we use the concat() function:

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

The evaluation of this XPath expression produces exactly the wanted result:

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

XSLT - based verification:

<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>

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>

the XPath expression is evaluated and the result of this evaluation is copied to the output:

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

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

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