使用XPath:找到根节点下每个段落的最后一个文本节点
我想修剪所有 XHTML 段落末尾的尾随空格。 我将 Ruby 与 REXML 库一起使用。
假设我在有效的 XHTML 文件中有以下内容:
<p>hello <span>world</span> a </p>
<p>Hi there </p>
<p>The End </p>
我想最终得到这样的结果:
<p>hello <span>world</span> a</p>
<p>Hi there</p>
<p>The End</p>
所以我想我可以使用 XPath 来获取我想要的文本节点,然后修剪文本,这将允许我最终得到我想要什么(上一个)。
我从以下 XPath 开始:
//root/p/child::text()
当然,这里的问题是它返回所有 p 标签的子节点的所有文本节点。 这是这样的:
'hello '
' a '
'Hi there '
'The End '
尝试以下 XPath 给出最后一段的最后一个文本节点,而不是作为根节点的子节点的每个段落的最后一个文本节点。
//root/p/child::text()[last()]
这只返回: 'The End '
因此,我想从 XPath 得到的是:
' a '
'Hi there '
'The End '
我可以用 XPath 做到这一点吗? 或者我应该考虑使用正则表达式(这可能比 XPath 更令人头疼)?
I want to trim trailing whitespace at the end of all XHTML paragraphs. I am using Ruby with the REXML library.
Say I have the following in a valid XHTML file:
<p>hello <span>world</span> a </p>
<p>Hi there </p>
<p>The End </p>
I want to end up with this:
<p>hello <span>world</span> a</p>
<p>Hi there</p>
<p>The End</p>
So I was thinking I could use XPath to get just the text nodes that I want, then trim the text, which would allow me to end up with what I want (previous).
I started with the following XPath:
//root/p/child::text()
Of course, the problem here is that it returns all text nodes that are children of all p-tags. Which is this:
'hello '
' a '
'Hi there '
'The End '
Trying the following XPath gives me the last text node of the last paragraph, not the last text node of each paragraph that is a child of the root node.
//root/p/child::text()[last()]
This only returns: 'The End '
What I would like to get from the XPath is therefore:
' a '
'Hi there '
'The End '
Can I do this with XPath? Or should I maybe be looking at using regular expressions (That's probably more of a headache than XPath)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的例子对我有用
Your example worked for me
以防万一您不知道,XSL 有一个
normalize-space()
函数,它将消除前导空格和尾随空格。Just in case you didn't know, XSL has a
normalize-space()
function which will get rid of leading and trailing spaces.