<DOC NUMBER=1>
<DOCFULL> -->
<br><div class="c0">
<p class="c1"><span class="c2">Dokument 1 von 3</span></p>
</div>
<br><div class="c0">
<br><p class="c1"><span class="c2">Associated Press Financial Wire</span></p>
</div>
<br><div class="c3">
<p class="c1"><span class="c2">April 25, 2012 Wednesday 9:18 PM GMT </span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c6">Apple CEO Tim Cook emerges from Steve Jobs' shadow</span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c7">BYLINE: </span><span class="c2">By PETER SVENSSON, AP Technology Writer</span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c7">SECTION: </span><span class="c2">BUSINESS NEWS</span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c7">LENGTH: </span><span class="c2">794 words</span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c7">DATELINE: </span><span class="c2">NEW YORK </span></p>
</div>
<br><div class="c4">
<p class="c8"><span class="c2"> MAIN TEXT 1</span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c7">LOAD-DATE: </span><span class="c2">April 26, 2012</span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c7">LANGUAGE: </span><span class="c2">ENGLISH</span></p>
</div>
<br><div class="c4">
<p class="c5"><span class="c7">PUBLICATION-TYPE: </span><span class="c2">Newswire</span></p>
</div>
<br><div class="c0">
<br><p class="c1"><span class="c2">Copyright 2012 Associated Press<br>All Rights Reserved</span></p>
</div>
<!-- Hide XML section from browser
</DOCFULL>
</DOC> -->
我是 xpath 的新手,我想将它与 R(Duncan Lang 的 xml 包)结合使用来查询我从 LexisNexis 收到的 html 文档。该文档包含多篇新闻文章,每篇文章都以<DOC NUMBER=1> <DOCFULL>
标签为界。我希望为每个文档提取一些信息,例如提取 SECTION 信息,我到此为止:
doc <- htmlParse("hmtldoc.HTML")
xpathSApply(doc,"//span[text()='SECTION: ']/..", xmlValue)
这给了我:
[1] "SECTION: BUSINESS NEWS" "SECTION: BUSINESS NEWS" "SECTION: BUSINESS NEWS"
那是我可以使用的输出。主要问题是并非每篇文章都有 SECTION 信息。我需要知道的是哪篇文章提供了这些信息,哪些没有,最好是通过返回 NA 或一个空的列表元素,这样我就可以自己推断出这些信息。
与这个问题相关:我试图提出一个解决方案,我首先选择 DOC 或 DOCFULL 节点并从那里继续,例如:
xpathSApply(doc,"//DOCFULL/*/span[text()='SECTION: ']/..", xmlValue)
我认为这应该返回与上面相同的文本,但事实并非如此。无论如何,我对这门语言还是很陌生,感谢任何帮助。