【发布时间】:2010-10-06 20:25:25
【问题描述】:
xml文件有这个sn-p:
<?xml version="1.0"?>
<PC-AssayContainer
xmlns="http://www.ncbi.nlm.nih.gov"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://www.ncbi.nlm.nih.gov ftp://ftp.ncbi.nlm.nih.gov/pubchem/specifications/pubchem.xsd"
>
....
<PC-AnnotatedXRef>
<PC-AnnotatedXRef_xref>
<PC-XRefData>
<PC-XRefData_pmid>17959251</PC-XRefData_pmid>
</PC-XRefData>
</PC-AnnotatedXRef_xref>
</PC-AnnotatedXRef>
我尝试使用 xpath 的全局搜索来解析它,还尝试了一些命名空间:
library('XML')
doc = xmlInternalTreeParse('http://s3.amazonaws.com/tommy_chheng/pubmed/485270.descr.xml')
>xpathApply(doc, "//PC-XRefData_pmid")
list()
attr(,"class")
[1] "XMLNodeSet"
> getNodeSet(doc, "//PC-XRefData_pmid")
list()
attr(,"class")
[1] "XMLNodeSet"
> xpathApply(doc, "//xs:PC-XRefData_pmid", ns="xs")
list()
> xpathApply(doc, "//xs:PC-XRefData_pmid", ns= c(xs = "http://www.w3.org/2001/XMLSchema-instance"))
list()
xpath 不应该匹配:
<PC-XRefData_pmid>17959251</PC-XRefData_pmid>
【问题讨论】:
-
对 R 一无所知,我假设
ns="xs"和ns= c(xs...部分声明了表达式中使用的命名空间。这可能是问题所在,因为元素PC-XRefData_pmid不是http://www.w3.org/2001/XMLSchema-instance命名空间的成员,而是http://www.ncbi.nlm.nih.gov,这是源文档中的默认命名空间。搜索xs:PC-XRefData_pmid是错误的。 -
我假设我不需要命名空间,因为默认的是 xmlns="ncbi.nlm.nih.gov"? xpath 查询“//PC-XRefData_pmid”不应该工作吗?