New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace aliases #35

Closed
jeroen opened this Issue May 25, 2015 · 5 comments

Comments

Projects
None yet
3 participants
@jeroen
Member

jeroen commented May 25, 2015

In this example borrowed form the rversions package lp1: and lp3: namespace are aliases of D: however xml_ns does not list them at all, making it difficult to query the document.

library(curl)
library(xml2)
h <- handle_setheaders(new_handle(customrequest = "PROPFIND"), Depth="1")
req <- curl_fetch_memory("http://svn.r-project.org/R/tags/", handle = h)
doc <- read_xml(rawToChar(req$content))
prop <- xml_find_all(doc, ".//D:propstat/D:prop", xml_ns(doc))

# does not work
xml_find_one(prop, ".//lp1:getetag", xml_ns(doc))

# works
xml_find_one(prop, ".//D:getetag", xml_ns(doc))
@hadley

This comment has been minimized.

Member

hadley commented May 25, 2015

Why does that make it hard to navigate the document? The goal of xml_ns is to provide a unique abbreviation for each namespace.

@jeroen

This comment has been minimized.

Member

jeroen commented May 25, 2015

I found it confusing that the node in the xml is named lp1:getetag but searching for this node gives:

> xml_find_one(prop, ".//lp1:getetag", xml_ns(doc))
xmlXPathEval: evaluation failed
Error: No matches
In addition: Warning message:
In node_find_one(x$node, x$doc, xpath = xpath, nsMap = ns) :
  Undefined namespace prefix [1219]

Only by manually inspecting the xml I found that lp1: is an alias of D: in this case.

@hadley

This comment has been minimized.

Member

hadley commented May 26, 2015

That's how xml namespaces work though - the prefix is completely arbitrary, it's only the url that identifies it.

But maybe xml_ns() shouldn't just return unique prefixes? Would need some testing - I'm not sure if the libxml's xpath code will allow the same url to be mapped to multiple prefixes.

@jimhester

This comment has been minimized.

Member

jimhester commented May 12, 2016

Having aliases to the same URI seems to work OK in limited testing. I think we should not return only unique prefixes.

library(curl)
library(xml2)
h <- handle_setheaders(new_handle(customrequest = "PROPFIND"), Depth="1")
req <- curl_fetch_memory("http://svn.r-project.org/R/tags/", handle = h)
doc <- read_xml(rawToChar(req$content))
ns <- xml_ns(doc)

# Add the additional alias
ns[["lp1"]] <- "DAV:"

prop <- xml_find_all(doc, ".//D:propstat/D:prop", ns)
xml_find_one(prop, ".//lp1:getetag", ns)
#> {xml_nodeset (141)}
#>  [1] <lp1:getetag>W/"70574//tags"</lp1:getetag>
#>  [2] <lp1:getetag>W/"10156//tags/R-before-GenGC-merge"</lp1:getetag>
#>  [3] <lp1:getetag>W/"371//tags/R-0-60"</lp1:getetag>
#>  [4] <lp1:getetag>W/"1452//tags/R-0-62-1"</lp1:getetag>
#>  [5] <lp1:getetag>W/"477//tags/R-0-61"</lp1:getetag>
#>  [6] <lp1:getetag>W/"1721//tags/R-0-62-2"</lp1:getetag>
#>  [7] <lp1:getetag>W/"1447//tags/R-0-62"</lp1:getetag>
#>  [8] <lp1:getetag>W/"2012//tags/R-0-62-3"</lp1:getetag>
#>  [9] <lp1:getetag>W/"2621//tags/R-0-62-4"</lp1:getetag>
#> [10] <lp1:getetag>W/"2838//tags/R-0-63"</lp1:getetag>
#> [11] <lp1:getetag>W/"4084//tags/R-0-64"</lp1:getetag>
#> [12] <lp1:getetag>W/"5635//tags/R-0-65"</lp1:getetag>
#> [13] <lp1:getetag>W/"8828//tags/R-1-0-1"</lp1:getetag>
#> [14] <lp1:getetag>W/"62482//tags/R-3-0-0"</lp1:getetag>
#> [15] <lp1:getetag>W/"62744//tags/R-3-0-1"</lp1:getetag>
#> [16] <lp1:getetag>W/"35751//tags/R-2-2-0"</lp1:getetag>
#> [17] <lp1:getetag>W/"36814//tags/R-2-2-1"</lp1:getetag>
#> [18] <lp1:getetag>W/"63988//tags/R-3-0-2"</lp1:getetag>
#> [19] <lp1:getetag>W/"18029//tags/R-1-4-1"</lp1:getetag>
#> [20] <lp1:getetag>W/"65127//tags/R-3-0-3"</lp1:getetag>
#> ...

# specifying the URI explicitly also works
xml_find_one(prop, ".//*[local-name()='getetag' and namespace-uri()='DAV:']")
#> {xml_nodeset (141)}
#>  [1] <lp1:getetag>W/"70574//tags"</lp1:getetag>
#>  [2] <lp1:getetag>W/"10156//tags/R-before-GenGC-merge"</lp1:getetag>
#>  [3] <lp1:getetag>W/"371//tags/R-0-60"</lp1:getetag>
#>  [4] <lp1:getetag>W/"1452//tags/R-0-62-1"</lp1:getetag>
#>  [5] <lp1:getetag>W/"477//tags/R-0-61"</lp1:getetag>
#>  [6] <lp1:getetag>W/"1721//tags/R-0-62-2"</lp1:getetag>
#>  [7] <lp1:getetag>W/"1447//tags/R-0-62"</lp1:getetag>
#>  [8] <lp1:getetag>W/"2012//tags/R-0-62-3"</lp1:getetag>
#>  [9] <lp1:getetag>W/"2621//tags/R-0-62-4"</lp1:getetag>
#> [10] <lp1:getetag>W/"2838//tags/R-0-63"</lp1:getetag>
#> [11] <lp1:getetag>W/"4084//tags/R-0-64"</lp1:getetag>
#> [12] <lp1:getetag>W/"5635//tags/R-0-65"</lp1:getetag>
#> [13] <lp1:getetag>W/"8828//tags/R-1-0-1"</lp1:getetag>
#> [14] <lp1:getetag>W/"62482//tags/R-3-0-0"</lp1:getetag>
#> [15] <lp1:getetag>W/"62744//tags/R-3-0-1"</lp1:getetag>
#> [16] <lp1:getetag>W/"35751//tags/R-2-2-0"</lp1:getetag>
#> [17] <lp1:getetag>W/"36814//tags/R-2-2-1"</lp1:getetag>
#> [18] <lp1:getetag>W/"63988//tags/R-3-0-2"</lp1:getetag>
#> [19] <lp1:getetag>W/"18029//tags/R-1-4-1"</lp1:getetag>
#> [20] <lp1:getetag>W/"65127//tags/R-3-0-3"</lp1:getetag>
#> ...
@hadley

This comment has been minimized.

Member

hadley commented May 25, 2016

Yeah, that sounds fine to me.

jimhester added a commit to jimhester/xml2 that referenced this issue May 31, 2016

jimhester added a commit to jimhester/xml2 that referenced this issue May 31, 2016

jimhester added a commit to jimhester/xml2 that referenced this issue May 31, 2016

jimhester added a commit to jimhester/xml2 that referenced this issue May 31, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment