Domanda

Update Thanks to florent I now realize I used the wrong term in the question (originally: 'What is the namespace of an XML document which does not have a namespace defined?').
Also, I think now I am asking two questions:
1. can one assign a prefix to an XML document which does not have a prefix? (probably yes, but how does one do this with classic ASP?)
2. what is the XPath location path of a node in an XML document which does not have a prefix defined? If this is even possible.


I have classic ASP code which retrieves an XML document using the MSXML2.ServerXMLHTTP.6.0 object, but the XML document does not have a prefix defined. I can't change the way the XML document is made.

This is how the XML looks:

<?xml version="1.0" standalone="yes"?>
<SearchResultsResponse xmlns="http://openapi.somesite.com/openapi-3.0.0">
[...]
</SearchResultsResponse>    


The document is automatically parsed and accessable trough the .responseXML property. But looking up nodes with:

Set objData = obj_http.responseXML.selectSingleNode("//someNodeName")
Response.Write "Data: " & objData.Text

doesn't work. (I get a 'Object Required' error message, meaning no node was found.) And

Response.Write obj_http.responseXML.documentElement.prefix 

gives me an empty string.


One way to make this work is to use .selectSingleNode("//*[local-name() = 'someNodeName']"), but I guess this is not very efficient in larger XML documents. (Or am I wrong there?)

I have read that something like: .selectSingleNode("//ns:rootNodeName/ns:childNodeName"), where 'ns' is the prefix defined, should be the way to go, but then what should I use for the prefix if none is defined?

È stato utile?

Soluzione 2

I finally figured it out:

Dim obj_XML
Set obj_XML = obj_http.responseXML

' this is where the magic happens, 'ns' is defined as the prefix:
obj_XML.setProperty _
    "SelectionNamespaces", _
    "xmlns:ns='http://openapi.somesite.com/openapi-3.0.0' " &_
    "xmlns:xs='http://www.w3.org/2001/XMLSchema'"

Dim obj_node
' now we can get the node with an XPath location path with the 'ns' prefix:
Set obj_node = obj_XML.selectSingleNode("//ns:SearchResultsResponse/ns:SessionId")

If obj_node Is Nothing Then
    Response.Write "# No node found #<br />"
Else
    Response.Write "Node text: " & obj_node.Text & "<br />"
End If

Altri suggerimenti

As far as I understand "http://openapi.somesite.com/openapi-3.0.0" is the namespace of the document. It just doesn't have a prefix defined so this is the default namespace.

I have read that something like: .selectSingleNode("//ns:rootNodeName/ns:childNodeName"), where 'ns' is the namespace defined, should be the way to go, but then what should I use for the namespace if none is defined?

"ns" here is not the namespace but the namespace prefix. I'm not an ASP person so I cannot help much with the code but you should still be able to define a namespace for your node lookup. You should even be able to redefine it with a prefix if you don't want to use the default namespace.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top