首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R XML包在解析xml和html文件时出现奇怪的错误

R XML包在解析xml和html文件时出现奇怪的错误
EN

Stack Overflow用户
提问于 2012-05-26 05:51:34
回答 1查看 404关注 0票数 1

我正在使用R的XML包从各种各样的html和xml文件中提取所有可能的数据。这些文件基本上是文档或构建属性或自述文件。

代码语言:javascript
复制
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE chapter PUBLIC '-//OASIS//DTD DocBook XML V4.1.2//EN'
                      'http://www.oasis-open.org/docbook/xml/4.0 docbookx.dtd'>

<chapter lang="en">
<chapterinfo>
<author>
<firstname>Jirka</firstname>
<surname>Kosek</surname>
</author>
<copyright>
<year>2001</year>
<holder>Ji&rcaron;&iacute; Kosek</holder>
</copyright>
<releaseinfo>$Id: htmlhelp.xml,v 1.1 2002/05/15 17:22:31 isberg Exp $</releaseinfo>
</chapterinfo>
<title>Using XSL stylesheets to generate HTML Help</title>
<?dbhtml filename="htmlhelp.html"?>

<para>HTML Help (HH) is help-format used in newer versions of MS
Windows and applications written for this platform. This format allows
to pack several HTML files together with images, table of contents and
index into single file. Windows contains browser for this file-format
and full-text search is also supported on HH files. If you want know
more about HH and its capabilities look at <ulink
url="http://msdn.microsoft.com/library/tools/htmlhelp/chm/HH1Start.htm">HTML
Help pages</ulink>.</para>

<section>
<title>How to generate first HTML Help file from DocBook sources</title>

<para>Working with HH stylesheets is same as with other XSL DocBook
stylesheets. Simply run your favorite XSLT processor on your document
with stylesheet suited for HH:</para>

</section>

</chapter>

我的目标是在使用htmlTreeParse或xmlTreeParse解析树之后使用xmlValue,使用如下代码(对于xml文件..)

代码语言:javascript
复制
Text = xmlValue(xmlRoot(xmlTreeParse(XMLFileName)))

但是,当我对xml和html文件执行此操作时,有一个错误。如果在级别2或更高级别上有子节点,则粘贴文本字段时,它们之间没有任何空格。

例如,在上面的示例中

xmlValue(chapterInfo)是

代码语言:javascript
复制
JirkaKosek2001JiKosek$Id: htmlhelp.xml,v 1.1 2002/05/15 17:22:31 isberg Exp 

将每个子节点(递归)的xmlValues粘贴在一起,而不在它们之间添加空格。如何让xmlValue在提取此数据时添加一个空格

非常感谢你事先的帮助,

Shivani

EN

回答 1

Stack Overflow用户

发布于 2012-05-26 07:46:46

根据文档,xmlValue只适用于单个文本节点,或者“包含单个文本节点的XML节点”。非文本节点中的空格显然没有保留。

但是,即使在单个文本节点的情况下,您的代码也会去掉空格。

代码语言:javascript
复制
library(XML)
doc <- xmlTreeParse("<a> </a>")
xmlValue(xmlRoot(doc))
# [1] ""

您可以将ignoreBlanks=FALSEuseInternalNodes=TRUE参数添加到xmlTreeParse,以保留所有空格。

代码语言:javascript
复制
doc <- xmlTreeParse(
  "<a> </a>", 
  ignoreBlanks = FALSE, 
  useInternalNodes = TRUE
)
xmlValue(xmlRoot(doc))
# [1] " "

# Spaces inside text nodes are preserved
doc <- xmlTreeParse(
  "<a>foo <b>bar</b></a>", 
  ignoreBlanks = FALSE, 
  useInternalNodes = TRUE
)
xmlValue(xmlRoot(doc))
# [1] "foo bar"

# Spaces between text nodes (inside non-text nodes) are not preserved
doc <- xmlTreeParse(
  "<a><b>foo</b> <b>bar</b></a>", 
  ignoreBlanks = FALSE, 
  useInternalNodes = TRUE
)
xmlValue(xmlRoot(doc))
# [1] "foobar"
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10761786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档