I have found in the past the handling of namespaces in both the XDOM
commands and the EXT files, is not well documented, and does not always work
how I expect.  So I usually preprocess any such XML files with xsltproc to
remove the namespaces - an example xsl to do this is 

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

 <!-- keep comments -->
 <xsl:template match="comment()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <!-- remove element prefix -->
    <xsl:element name="{local-name()}">
      <!-- process attributes -->
      <xsl:for-each select="@*">
        <!-- remove attribute prefix -->
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>


-----Original Message-----
From: u2-users-boun...@listserver.u2ug.org
[mailto:u2-users-boun...@listserver.u2ug.org] On Behalf Of Peter Cheney
Sent: 06 November 2013 02:01
To: U2 Users List
Subject: [U2] UV Basic parsing xml with XDOM commands

Hi Everyone,

Thought I'd post a solution to a task I've been working on in the hope it
will assist someone else with similar requirements. Thanks also to those
who've previously posted on this as I found the following to be most helpful
as well:
http://www.mail-archive.com/u2-users@listserver.u2ug.org/msg18895.html
https://gdoesu2.wordpress.com/tag/xdomlocate/
http://listserver.u2ug.org/pipermail/u2-users/2010-September/004713.html
http://www.mvdeveloper.com/kb/docs/kb10.pdf


I had to parse some xml returned via a soap request to an IIS server:
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/";>
  <s:Body>
    <SuggestResponse xmlns="http://tempuri.org/";>
      <SuggestResult
xmlns:a="http://schemas.datacontract.org/2004/07/FirstMac.Services.Category.
Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance";>
        <a:CategoryGroup>Some group data</a:CategoryGroup>
        <a:CategoryName>Some name data</a:CategoryName>
      </SuggestResult>
    </SuggestResponse>
  </s:Body>
</s:Envelope>
There are only ever 2 value nodes returned if the soap query is successful
with the SuggestResult node containing 'i:nil="true"' if the query is not
successful.
e.g. <SuggestResult i:nil="true" ... />

The xml contains a default namespace and after much searching and trial and
error I discovered I needed to use the following xpathString and nsMAP
parameters in the XDOMLocate function:
Notes re the XDOMLocate function.
I found the UV11.1 Basic Commands Reference and Basic Extensions documents
for XDOMLocate to be scarce on detail for the following:

  *   Each additional namespace in the string must also be space delimited.
  *   The double quotes enclosing the http url must be removed for each
namespace defined in the nsMAP parameter. The description given has double
quotes in the wrong places. e.g. if you have <s:Envelope
xmlns:s="http://schemas.xmlsoap.org/soap/envelope/";> then the namespace you
need to specify is nsMAP =
'xmlns:s=http://schemas.xmlsoap.org/soap/envelope/'

<code>
xpathStr  = '/s:Envelope/s:Body'

nsMAP  = 'xmlns:s=http://schemas.xmlsoap.org/soap/envelope/ '
nsMAP :=
'xmlns:a=http://schemas.datacontract.org/2004/07/FirstMac.Services.Category.
Contracts '
nsMAP := 'xmlns=http://tempuri.org/ '
nsMAP := 'xmlns:i=http://www.w3.org/2001/XMLSchema-instance'

OK = XDOMOpen( RespData, XML.FROM.STRING, hDOM ) if OK = XML.SUCCESS then
   crt 'XML document opened'
   OK = XDOMLocate( hDOM, xpathStr, nsMAP, hRoot )
   if OK = XML.SUCCESS then
      crt 'Found ':xpathStr

      OK = XDOMGetNodeName( hRoot, NodeName )
      crt 'Root node=':NodeName
      OK = XDOMLocateNode( hRoot, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hParent )
      if OK = XML.SUCCESS then
         OK = XDOMGetNodeName( hParent, NodeName )
         crt 'Parent node = ':NodeName                         ;*
'SuggestResponse'
         OK = XDOMLocateNode( hParent, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hChild )
         if OK = XML.SUCCESS then
            OK = XDOMGetNodeName( hChild, NodeName )
            crt 'Child node = ':NodeName                       ;*
'SuggestResult'
            OK = XDOMLocateNode( hChild, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.ELEMENT.NODE, hValueNode )
            if OK = XML.SUCCESS then
               OK = XDOMGetNodeName( hValueNode, NodeName )
               crt 'Value node1 = ':NodeName                   ;*
'a:CategoryGroup'
               OK = XDOMLocateNode(hValueNode, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.TEXT.NODE, hTextNode )
               if OK = XML.SUCCESS then
                  OK = XDOMGetNodeValue(hTextNode, Value )
                  crt 'Payload1 = ':Value                      ;* 'Some
group data'
               end else
                  gosub ShowError
               end
            end
            OK = XDOMLocateNode( hChild, XDOM.CHILD, 2, XDOM.ELEMENT.NODE,
hValueNode )
            if OK = XML.SUCCESS then
               OK = XDOMGetNodeName( hValueNode, NodeName )
               crt 'Value node2 = ':NodeName                   ;*
'a:CategoryName'
               OK = XDOMLocateNode(hValueNode, XDOM.CHILD, XDOM.FIRST.CHILD,
XDOM.TEXT.NODE, hTextNode )
               if OK = XML.SUCCESS then
                  OK = XDOMGetNodeValue(hTextNode, Value )
                  crt 'Payload2 = ':Value                      ;* 'Some name
data'
               end else
                  gosub ShowError
               end
            end else
               gosub ShowError
            end
         end else
            gosub ShowError
         end
      end else
         gosub ShowError
      end
   end
   OK = XDOMClose(hDOM)
end
return
*
ShowError:
OK = XMLGetError(Code, ErrText)
Crt "Code = ":Code
Crt "Text = ":ErrText
return
</code>

Program Output:
XML document opened
Found /s:Envelope/s:Body
Root node=s:Body
Parent node = SuggestResponse
Child node = SuggestResult
Value node1 = a:TagGroup
Payload1 = Household expenses
Value node2 = a:TagName
Payload2 = Telephone / mobile


A brief explanation of the above code snippet:

  1.  Open the xml document with XDOMOpen
  2.  Obtain a handle to the root node based on the specified xpathString
and nsMAP parameters
  3.  Display the root node name
  4.  Obtain a handle to the parent node
  5.  Display the parent node name
  6.  Obtain a handle to the child node
  7.  Display the child node name
  8.  Obtain a handle to the 1st value node
  9.  display the 1st value node name
  10. Obtain a handle to the text node
  11. Display the 1st text data
  12. Obtain a handle to the 2nd (next) value node
  13. Display the 2nd value node name
  14. Obtain a handle to the 2nd text node
  15. Display the 2nd text data


Cheers
Peter






[Description: Description:
https://www.firstmac.com.au]<https://webmail.firstmac.com.au/owa/redir.aspx?
C=3TxBWxfLckGBVPoLiIA9bpVACaOTOtAI5wx873XG4iVimMG4ihRNvq2GjE8ncHzAUsu8CiQ8Fd
M.&URL=http%3a%2f%2fwww.firstmac.com.au%2f>

Peter Cheney

Universe Engineer

t 07 3017 8837 | f 07 3002 8400

e
peter.che...@firstmac.com.au<https://webmail.firstmac.com.au/owa/redir.aspx?
C=3TxBWxfLckGBVPoLiIA9bpVACaOTOtAI5wx873XG4iVimMG4ihRNvq2GjE8ncHzAUsu8CiQ8Fd
M.&URL=mailto%3apeter.cheney%40firstmac.com.au> | w
www.firstmac.com.au<https://webmail.firstmac.com.au/owa/redir.aspx?C=3TxBWxf
LckGBVPoLiIA9bpVACaOTOtAI5wx873XG4iVimMG4ihRNvq2GjE8ncHzAUsu8CiQ8FdM.&URL=ht
tps%3a%2f%2fwww.www.firstmac.com.au%2f>



 
----------------------------------------------------------------------------
---
Note: 
This email (inc all attachments) is for the use of the intended recipient(s)
only.
Privileged or confidential information may be contained in this
communication. If you have received this email in error, please notify the
sender immediately and then delete all copies of this message from your
computer network. If you are not the intended recipient, you must not keep,
use, disclose, copy or distribute this email without the author's prior
permission. If you are the intended recipient and you do not wish to receive
similar electronic messages from us in future, then please respond to the
sender to this effect. 
We have taken precautions to minimise the risk of transmitting software
viruses, but advise you to carry out your own virus checks on this email and
its attachments. We do not accept liability for any loss or damage caused by
software viruses and do not represent that this transmission is free from
viruses or other defects. 
Firstmac Limited (ABN 59 094 145 963) (AFSL 290600)
 
----------------------------------------------------------------------------
---
_______________________________________________
U2-Users mailing list
U2-Users@listserver.u2ug.org
http://listserver.u2ug.org/mailman/listinfo/u2-users

_______________________________________________
U2-Users mailing list
U2-Users@listserver.u2ug.org
http://listserver.u2ug.org/mailman/listinfo/u2-users

Reply via email to