1

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

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>

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.

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

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

It returns following error:

XPath error : Undefined namespace prefix xmlXPathCompOpEval: parameter error XPath error : Invalid expression Erreur dans xpathApply.XMLInternalDocument(doc, path, fun, ..., namespaces = namespaces, : error evaluating xpath expression data(//tei:name/@ref[starts-with(., 'Actr')])

How do you define namespace in this case?

2
  • I'm not familiar with R, but it looks like it doesn't recognize the namespace associated with the tei: prefix. If there's a way to declare a namespace in R, try that; otherwise use the wildcard for that element //*:name/@ref[...]. Commented Jun 22, 2015 at 16:01
  • Please read up on the differences between XPath and XQuery (which is a superset of XPath), and use the right tags. xpathSApply does not support XQuery (which is not a problem, you're only using a simple XPath expression in your question). Commented Jun 22, 2015 at 16:27

1 Answer 1

3

The XML package doesn't handle default namespaces very well. You need to be explicit about assigning a name to the namespace before you can use xpath style expressions. How about something like

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. There's one bracket missing at the end though. It returns the list of all the attribute's values beginning with "Actr". Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.