Skip to content
Prev 172216 / 398506 Next

Need help extracting info from XML file using XML package

Duncan Temple Lang wrote:
not really:  the xpath pattern '//coordinates' does say 'find all
coordinates nodes searching from the root', but the root here is not the
original root of the whole document, but each polygon node in turn. 

try:

    root = xmlInternalTreeParse('
        <root>
            <foo>
                <bar>1</bar>
            </foo>
            <foo>
                <bar>2</bar>
            </foo>
        </root>')

    xpathApply(root, '//foo', xpathSApply, '//bar', xmlValue)
    # equals list("1", "2"), not list(c("1", "2"), c("1", "2"))

this is not equivalent to

    xpathApply(root, '//foo', function(foo) xpathSApply(root, '//bar',
xmlValue))

but to

    xpathApply(root, '//foo', function(foo) xpathSApply(foo, '//bar',
xmlValue))


as the author of the XML package, you should know ;)
yes, in this case it would;  i was not sure about the concrete schema. 
i copied the code from my solution to some other problem, where polygon
would have multiple coordinates nodes which would have to be merged in
some way for each polygon separately -- your solution would return the
content of each coordinates nodes separately irrespectively of whether
it is unique within the polygon (which might well be in this particular
case, and thus your solution is undeniably more elegant).


vQ