0

i'm trying to get the node value in an XML Response. I'm very new to ASP.

Here is the XML:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
   <soap:Body>
      <ns2:getTextoTsjResponse xmlns:ns2="http://x.com/x/act" xmlns:ns3="http://i.e.com" xmlns:ns4="http://comun.e.com">
         <return>
            <ns3:texto>
               <ns3:datos>
                  <xop:Include href="cid:[email protected]" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
               </ns3:datos>
               <ns3:extension>pdf</ns3:extension>
            </ns3:texto>
            <ns3:textoAnonimizado>
               <ns3:datos>
                  <xop:Include href="cid:[email protected]" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
               </ns3:datos>
               <ns3:extension>pdf</ns3:extension>
            </ns3:textoAnonimizado>
         </return>
      </ns2:getTextoTsjResponse>
   </soap:Body>
</soap:Envelope>

I succeded geting the XMLDoc and parse. What im trying to do then is get href values within node.

Here is the ASP code.

'Response from server with the XML
xmlParseado = response.Text
Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument")
xmlDoc.loadXML(xmlParseado) 

if xmlDoc.parseError.errorcode <> 0 then
  Response.Write("XML Error...<br>")
else

  Call xmlDoc.setProperty("SelectionLanguage", "XPath")
  Call xmlDoc.setProperty("SelectionNamespaces", "xmlns:ns3")

  Dim node 
 'Here im trying to get href value from <ns3:datos> node.

  Set node = xmlDoc.selectSingleNode("//ns3:texto//ns3:datos//*")

  If (node Is Nothing) Then  
    Response.write "nothing"
  Else

  response.write(TypeName(node) & "<br />")

  End If

end if

Some help would be apreciated.

Thanks in advance

1 Answer 1

1

You are really close.

Sadly your XML (like many poorly designed XML documents) suffers from namespace overkill. If you think of elements like data files on a file system, these XML designers are paranoid that two files (elements) will have the same name, so they put every file (element) on a separate disk instead of just using folders (nesting elements) for context. In my entire career, I've never run into an issue with two files having the same name, but every day, there's a post on Stackoverflow from someone having trouble with namespaces.

So think of namespaces like a separate disk in a filesystem. The document specifies each "disk" with a nick name (the prefix like ns3), and a location (the URI like http://i.e.com). Now when you're querying XML with XPath, have to tell the parser about all of the namespaces (disks), and you specify the prefix in front of every element on the "disk". You're XPath will look like "//ns3:datos", not just "//datos".

So armed with namespaces, they became the "solution" for everything. When SOAP was created, the designers went crazy with namespaces, and as a result, everyone put namespaces in everything. The designers of SOAP must have been worried that their Header, Envelope and Body element names would be re-used in SOAP message data. Instead of naming the elements "SOAP_Header", "SOAP_Envelope", and "SOAP_Body" that everyone could remember, they implemented SOAP with XML namespaces just in case the message content accidentally also included an element named "Header", "Envelope" or "Body".

When you set the SelectionNamesSpaces, you need to set both the prefix and the URI. In this example I have all the namespaces in your document.

Good luck with your project, and please join me in killing namespaces where ever possible :)

if xmlDoc.parseError.errorcode <> 0 then
    Response.Write("XML Error...<br>")
    ' NOTE: Avoid nesting else statements, just stop.
    Response.End
End If


Call xmlDoc.setProperty("SelectionLanguage", "XPath")

' NOTE: you're missing the URI here in your namespace http://i.e.com
' Call xmlDoc.setProperty("SelectionNamespaces", "xmlns:ns3")

' Add all of the namespaces, including the prefix and URI
Call xmlDoc.setProperty("SelectionNamespaces", "xmlns:xop='http://www.w3.org/2004/08/xop/include' xmlns:ns2='http://x.com/x/act' xmlns:ns3='http://i.e.com' xmlns:ns4='http://comun.e.com'")

'Here im trying to get href value from <ns3:datos> node.
Dim node 
Set node = xmlDoc.selectSingleNode("//ns3:texto//ns3:datos//xop:Include")
If (node Is Nothing) Then  
    Response.write "nothing"
Else
    Response.write( node.getAttribute( "href" ) )
End If

' Here's how to get all of the Includes
Dim ndList
set ndList = xmlDoc.selectNodes( "//xop:Include" )
for each ndInclude in ndList
    Response.write( ndInclude.getAttribute( "href" ) )
next
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! i will do that.

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.