XPath 在 R 中的 XML 文档上失败 [英] XPath fails on an XML document in R

查看:34
本文介绍了XPath 在 R 中的 XML 文档上失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴能找到有关使用 R 进行 XML 操作的帮助.

I'll be glad to find some help with XML manipulation with R.

我正在尝试对我的 XML/TEI 文件进行 XPath.这是它的结构:

I'm trying to proceed XPath on my XML/TEI file. Here's its structure :

<?xml version="1.0" encoding="UTF-8"?>
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<text>
    <body>
        <div>
            <p>
                <seg>
                        <name ref="Actr1235">Jen B.</name>frate M. <name ref="Actr1234">Léard B.</name> rhoncus orci quis luctus ultrices <note place="margin-left">1713 &amp; 1714</note>, a été
                        vehicula cursus nunc, at sagittis lorem aliquet sed <name ref="Actr1236"> Jaes L.</name>
                        aeman graeca <name type="place">Digo</name> iaculis volutpat risu <name ref="Cole14">la
                        Charias</name>. M. <name ref="Actr1236">Laure</name> bibendum augue erat, fermentum semper. M. <name ref="Actr1235">B.</name> bibendum augue erat, fermentum semper
                </seg>
            </p>
        </div>
    </body>
</text>
</TEI>

</TEI>

I'd like to extract all the attribute's values beginning with "Actr" inside <name> tags. I've tried this XPath in an XMLeditor //tei:name/@ref[starts-with(., 'Actr')] and it's working.

我想提取 标签内以Actr"开头的所有属性值.我已经在 XMLeditor //tei:name/@ref[starts-with(., 'Actr')] 中尝试了这个 XPath 并且它正在工作.

Now, I'm trying to do it with R to put the query's results in a dataframe, using XML package to parse the document

现在,我正在尝试使用 R 将查询结果放入数据框中,使用 XML 包解析文档

library(XML) data1715<-xmlParse("My_document.xml") name_query<-xpathSApply(data1715, "data(//tei:name/@ref[starts-with(., 'Actr')])", xmlValue)

It returns following error: 

它返回以下错误:

XPath 错误:未定义的命名空间前缀 xmlXPathCompOpEval:参数错误 XPath 错误:无效的表达式 Erreur dansxpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces =命名空间,:评估 xpath 表达式时出错数据(//tei:name/@ref[starts-with(., 'Actr')])

How do you define namespace in this case?

在这种情况下,您如何定义命名空间?

解决方案

推荐答案

XML 包不能很好地处理默认命名空间.在您可以使用 xpath 样式表达式之前,您需要明确地为命名空间分配一个名称.怎么样像

xpathSApply(data1715, "//tei:name/@ref[starts-with(.,'Actr')]", unname, namespaces=c(tei=getDefaultNamespace(data1715)[[1]]$uri))

Note I also removed data() and changed xmlValue. I'm not sure what you were trying to do with data(), but here we are returning attributes and xmlValue doesn't appear to like working with attributes.

这篇关于XPath 在 R 中的 XML 文档上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆