【发布时间】:2014-01-17 22:37:16
【问题描述】:
我正在编写一个可以读取数学方程式的文本转语音程序。我有一个线程需要提取数学方程(如 MathJax SVG)并将它们解析为散文。
由于内容的布局方式,数学方程式可以任意嵌套在其他元素中,如段落、粗体、表格等。
使用对当前元素的引用,我如何获得下一个<span class="MathJax_SVG">,它可能嵌入在其他父/祖先中?
我尝试使用以下方法解决它:
nextMath = currentElement.xpath('following::.//span[@class=\'MathJax_SVG\']')
什么也不返回,尽管我可以从视觉上确认它后面有东西。我尝试删除句点,但 lxml 抱怨我的 XPath 格式不正确。
你们以前遇到过这种情况吗?
附:这是一个测试文档来说明我的观点:
<html>
<head>
<title>Test Document</title>
</head>
<body>
<h1 id="mainHeading">The Quadratic Formula</h1>
<p>The quadratic formula is used to solve quadratic equations. Here is the formula:</p>
<p><span class="MathJax_SVG" id="MathJax_Element_Frame_1">removed the SVG</span></p>
<p>Here are some possible values when you use the formula:</p>
<p>
<table>
<tr>
<td><span class="MathJax_SVG" id="MathJax_Element_Frame_2">removed the SVG</span></td>
<td><span class="MathJax_SVG" id="MathJax_Element_Frame_3">removed the SVG</span></td>
</tr>
<tr>
<td><span class="MathJax_SVG" id="MathJax_Element_Frame_4">removed the SVG</span></td>
<td><span class="MathJax_SVG" id="MathJax_Element_Frame_5">removed the SVG</span></td>
</tr>
</table>
</p>
</body>
</html>
更新
了解到lxml 不支持绝对位置。这可能是相关的。
一些测试代码(假设您将 HTML 保存为 test.html)
from lxml import html
# Get my html element
with open('test.html', 'r') as f:
myHtml = html.fromstring(f.read())
# Get the first MathJax element
start = myHtml.find('.//h1[@id=\'mainHeading\']')
print 'My start:', html.tostring(start)
# Get next math equation
nextXPath = 'following::.//span[@class=\'MathJax_SVG\']'
nextElem = start.xpath(nextXPath)
if len(nextElem) > 0:
print 'Next equation:', html.tostring(nextElem[0])
else:
print 'No next equation...'
【问题讨论】:
-
似乎
following在使用子元素时的行为与following-siblings完全相同。这应该是一个错误,因为子元素引用了它们的父元素。