-
Notifications
You must be signed in to change notification settings - Fork 83
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
Error on creating new XML - Expecting an external pointer #239
Comments
I have the same problem creating documents with a namespace prefix on the root node. My workaround is to create a suitable string and then read that in to start with: body <- xml2::read_xml('<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" />')
xml2::xml_add_child(body, "soap12:Body")
body
With some pointers how to start I'd be happy to attempt a PR to fix this. My first guess it the Line 125 in 7ed7218
|
Hey @nuest your solution seems way more elegant than mine. I've been creating an empty XML Body just containing a simple Body tag and later I replace it for soap12:Body using Anyway, it's been working here as a workaround but I'd love to contribute to fix this on xml2 package. Cheers soap_body_xml() %>%
xml_add_child(method, xmlns = "http://ultraasp.net/") %>%
xml_add_child("domain", domain) %>%
xml_add_sibling("sessionToken", session_token) %>%
xml_add_sibling("xmlMessage", xml_message) %>%
xml_root() %>%
as("character") %>%
str_replace_all("Envelope", "soap12:Envelope") %>%
str_replace_all("Body", "soap12:Body") |
I have gone through the code and it looks like the problem is exactly what you pointed. There is a problem when the namespace is being looked up. My guess is that there is no point in validate that if the parent node is not a node, right? Like when creating a root node. I've tried the following solution, just adding a new condition to ignore the ns_lookup call if the parent$node is null. What do you think? It might be too simplistic but it could fix the problem. :) parts <- strsplit(.value, ":")[[1]]
if ((length(parts) == 2) & !is.null(parent$node)) {
namespace <- ns_lookup(parent$doc, parent$node, parts[[1]])
node <- structure(list(node = node_new_ns(parts[[2]], namespace), doc = parent$doc), class = "xml_node")
} else {
node <- structure(list(node = node_new(.value), doc = parent$doc), class = "xml_node")
} |
I know that for adding a node, you can use library(magrittr)
library(xml2)
root <- xml_new_root( .value = "Envelope",
"xmlns:xsi" = "http://www.w3.org/2001/XMLSchema-instance",
"xmlns:xsd" = "http://www.w3.org/2001/XMLSchema",
"xmlns:soap12" = "http://www.w3.org/2003/05/soap-envelope")
xml_name(root) <- "soap12:Envelope"
root %>%
xml_add_child("Body") %>%
xml_set_namespace("soap12") %>%
xml_root()
#> {xml_document}
#> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
#> [1] <soap12:Body/> Created on 2019-03-31 by the reprex package (v0.2.1.9000) |
Issue Description and Expected Result
When trying to create a new XML body and it has a namespace defined on the root node, xml2 returns an error, even that the namespaces are correctly assigned. It just happens with the root node. An example of a situation like that is a SOAP request XML. The root node will be called soap:Envelope for soap 1.1 or soap12:for soap 1.2.
Reproducible Example
Created on 2018-11-06 by the reprex package (v0.2.1)
-->
Session Info
The text was updated successfully, but these errors were encountered: