Using Namespaces

Namespaces are represented in the xmlTree API as xmlNs structures, which contain prefix/URI pair for the namespace. Only one xmlNs structure exists per namespace, and all nodes that use that namespace point back to that namespace structure.

xmlTreeGetNsDef() gets the namespaces that are defined on that node, which could be more than one. xmlTreeGetNs() gets the namespace that applies to that node, which can only be one.

Consider the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<top xmlns:myns="urn:exampleNs"
    <myns:item>I'm an item!</myns:item>
    <myns:item>Me too!</myns:item>

There are two xmlNs structure in this document, both of which are defined on the <top> element. Therefore, calling xmlTreeGetNsDef() on <top> would return one xmlNs, and the second could be retrieved by calling xmlTreeGetNext() on the first. The <top> element defines them, but does NOT use either of them (it is in the default, empty namespace), so calling xmlTreeGetNs() on <top> will return nothing.

The <item> nodes do not declare any namespaces themselves, so xmlTreeGetNsDef() will return nothing for both of them. They both use the urn:exampleNs namespace, so calling xmlTreeGetNs() will return the xmlNs for urn:exampleNs.

See xmlTree04-Ns and xmlTree05_SetNS for examples of working with namespaces in the xmlTree API.

Copyright © Thunderstone Software     Last updated: Apr 15 2024
Copyright © 2024 Thunderstone Software LLC. All rights reserved.