Re: [U2] UV Basic parsing xml with XDOM commands
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:CategoryGroupSome group data/a:CategoryGroup a:CategoryNameSome 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
Re: [U2] UV Basic parsing xml with XDOM commands
Now that is quite a dance! [whew...] Bill Untitled Page - Original Message - *From:* peter.che...@firstmac.com.au *To:* U2 Users List u2-users@listserver.u2ug.org *Date:* 11/5/2013 6:00 PM *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:CategoryGroupSome group data/a:CategoryGroup a:CategoryNameSome 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
[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:CategoryGroupSome group data/a:CategoryGroup a:CategoryNameSome 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