2

I'm trying to use the XDOM functions of UniVerse to parse an XML file, but I can't get it to correctly parse XML that uses a default namespace. It can correctly handle XML without namespaces, or with named namespaces, but if there is a default namespace, all xPaths will fail to locate the nodes they are supposed to match.

To give a simple example, I'm, trying to parse this XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore xmlns="http://www.example.com">
   <book category="COOKING">
      <title lang="en">Everyday Italian</title>
      <author>Giada De Laurentiis</author>
      <year>2005</year>
      <price>30.00</price>
   </book>
   <book category="CHILDREN">
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
   </book>
   <book category="WEB">
      <title lang="en">Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
   </book>
</bookstore>

With this code:

PROGRAM XDOM.TEST
$INCLUDE SYSCOM XML.H

   OPEN "XML" TO F.XML ELSE STOP "OPEN FAILED"
   READ XML FROM F.XML, 'TEST.xml' ELSE STOP "READ FAILED"

   EXIT.PROG = @FALSE

   CONVERT @FM TO CHAR(10) IN XML
   IF NOT(EXIT.PROG) AND XDOMOpen(XML, XML.FROM.STRING, XDOM) # XML.SUCCESS THEN GOSUB XML.ERR
   IF NOT(EXIT.PROG) AND XDOMLocate(XDOM, '/bookstore/book[@category="CHILDREN"]', 'xmlns=http://www.example.com', XNODE) # XML.SUCCESS THEN GOSUB XML.ERR
   IF NOT(EXIT.PROG) AND XDOMEvaluate(XNODE, './author', 'xmlns=http://www.example.com', AUTHOR) # XML.SUCCESS THEN GOSUB XML.ERR
   IF NOT(EXIT.PROG) then PRINT AUTHOR
STOP

XML.ERR:

   XML.CODE = ''
   XML.ERR = ''
   EXIT.PROG = @TRUE
   IF XMLGetError(XML.CODE, XML.ERR) = XML.SUCCESS THEN
      PRINT XML.CODE
      PRINT XML.ERR
   END

   RETURN
END

When I run this code as is, I get the output:

10
The location path '/bookstore/book[@category="CHILDREN"]' was not found.

However, if I remove the "xmlns=http://www.example.com" namespace, it works fine.

Nate Allen
  • 3,159
  • 1
  • 11
  • 11

1 Answers1

2

After searching on my own, I found out that this is actually a bug UniVerse's XDOM parser itself. Someone has kindly documented a work-around here. You can "fool" the parser into working by giving a name to the default namespace. They also note that you can't use double-quotes in the namespace map either.

Personally, I prefer the simpler solution of just manually stripping out the offending namespace before parsing it. Adding this line to the above program fixes the issue, albeit in a very hacky way:

XML = CHANGE(XML, ' xmlns="http://www.example.com"', '')

This way you don't have to put unnecessary prefixes on all your xPath nodes. This may not always be an option though, depending on how you are getting the XDOM handle.

Nate Allen
  • 3,159
  • 1
  • 11
  • 11
  • Thanks for providing the various solutions. I went with the solution in the link you provided since it did not involve altering the XML document, but both would work fine. – Shawn McKnight May 16 '13 at 23:44