我正在使用XML包来提取一些元数据。我找到了一个很好的解决方案来提取作者姓名:https://www.r-bloggers.com/microsoft-office-metadata-with-r/。 使用代码的第一行,我得到了:
library(XML)
doc <- xmlInternalTreeParse('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dc:title>CHR-nr</dc:title>
<dc:subject/>
<dc:creator>XXXXXX</dc:creator>
<cp:keywords/>
<dc:description/>
<cp:lastModifiedBy>XXXXXX</cp:lastModifiedBy>
<cp:revision>1</cp:revision>
<cp:lastPrinted>2013-03-22T12:16:00Z</cp:lastPrinted>
<dcterms:created xsi:type="dcterms:W3CDTF">2013-03-22T12:13:00Z</dcterms:created>
<dcterms:modified xsi:type="dcterms:W3CDTF">2013-03-22T12:24:00Z</dcterms:modified>
</cp:coreProperties>', asText=TRUE)
我想从这些行中提取信息:
<dcterms:created xsi:type="dcterms:W3CDTF">2013-03-22T12:13:00Z</dcterms:created>
<dcterms:modified xsi:type="dcterms:W3CDTF">2013-03-22T12:24:00Z</dcterms:modified>
使用以下代码(我将使用最后一行作为示例):
ns=c('dcterms'= 'http://purl.org/dc/elements/1.1/')
date = xmlValue(getNodeSet(doc, '/*/dcterms:modified\ xsi:type=\"dcterms:W3CDTF\"', namespaces=ns)[[1]])
但是我收到了这个错误:
XPath error : Invalid expression
/*/dcterms:modified xsi:type="dcterms:W3CDTF"
^
Error in xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, :
error evaluating xpath expression /*/dcterms:modified xsi:type="dcterms:W3CDTF"
>
有人可以帮忙写出正确的路径吗?
答案 0 :(得分:0)
看起来您没有正确设置命名空间。您需要同时设置dcterms
和xsi
,并且xpath命名空间中dcterms
的URL必须与原始XML文档中使用的URL相同。另外你的XPATH按属性过滤有点过了。试试
ns <- c('dcterms'= 'http://purl.org/dc/terms/',
'xsi'="http://www.w3.org/2001/XMLSchema-instance")
xq <- '/*/dcterms:modified[@xsi:type=\"dcterms:W3CDTF\"]'
date <- xmlValue(getNodeSet(doc, xq, namespaces=ns)[[1]])