Re: [xml] Preserving namespace prefix when doc does not contain proper NS declaration



Interesting thats the similar results from my mini
program I wrote to look into tihs issue.  Let me show
an example and see if I can explain myself better

Say I have a chunk of xml I need to parse:

<item>
  <media:enclosure id="123">http://a.b.com />
  <title> This is a title </title>
</item>

Now in this example, the media namespace is not
defined, but for my purposes thats ok and I was
wondering if there was some way to access "media"
prefix when I'm iterating through the xmlNode tree
that the parser returns.  As near as I can tell, the
parser parses fine, outputting errors (which I can
turn off) but all traces of "media" are gone.

Am I making sense?

Shane


Ps - thanks for the reply.

--- Daniel Veillard <veillard redhat com> wrote:

On Wed, Jul 20, 2005 at 05:05:37PM -0700, Shane
Adams wrote:
Hi - I searched the archives and looked at the
API,
hope I'm not missing something obvious.  I'm
parsing
RSS feeds, alot of feeds contain extensions. 
These
extra tags should be declared by a proper
namespace
declaration, but in many cases they are not.

Is there a way to force libxml2 to parse the
document,

   Yes libxml2 will parse the cocument if it is
well-formed for XML-1.0
you will get non-fatal namespace errors though.

and preserve the 'prefix' to a namespace that is
not
defined?

   I don't understand what it means. Libxml2
behaviour is the following:

laptop:~/XML -> cat tst.xml
<foo>
  <present:bar xmlns:present="http://example.com"/>
  <missing:bar/>
</foo>

laptop:~/XML -> xmllint --debug tst.xml
tst.xml:3: namespace error : Namespace prefix
missing on bar is not defined
  <missing:bar/>
              ^
DOCUMENT
version=1.0
URL=tst.xml
standalone=true
  ELEMENT foo
    TEXT
      content=
    ELEMENT present:bar
      namespace present href=http://example.com
    TEXT
      content=
    ELEMENT bar
    TEXT
      content=

  seems the prefix part of the element name is
missing, looks like a bug.
But libxml2 should not "invent" a namespace.

Daniel

-- 
Daniel Veillard      | Red Hat Desktop team
http://redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit
 http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine
http://rpmfind.net/





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]