| ||||||||
XML Namespaces and CMarkupXML Namespaces are an attempt by the creators of the XML standard to systematize the development of markup vocabularies of element and attribute names (see Namespaces in XML for the motivation behind XML namespaces and all the gory details). The bottom line is that XML Namespaces often become an obstacle to developers needing to deploy applications and parse existing documents, but CMarkup allows you to overcome these obstacles painlessly. The Quickest XML Namespaces Intro EverXML Namespaces by Example, Tim Bray on XML.com, is a great quick start, but I'll make it even quicker and throw in default namespaces to boot. In the root element of the document you will often find attributes that declare namespaces such as: <h:html xmlns:h="http://www.w3.org/HTML/1998/html4"
xmlns:xdc="http://www.xml.com/books">
This declares the <xdc:title h:style="font-weight:bold;">
There is also a default namespace (declared with the <html xmlns="http://www.w3.org/HTML/1998/html4">
<head><title>Book Review</title></head>
<body>
<bookreview xmlns="http://www.xml.com/books">
<title>XML: A Primer</title>
That is basically all there is to it! Well, there are some complications, like default namespaces not applying to attributes, and the issue of an element from one vocabulary having an attribute from another was hinted in the example with the Programming XML NamespacesWell, a soon as you try to hide the complexity of colliding vocabularies from the developer, you are sure to cause twice the grief. Aside from making XML less human readable, the main problem with XML Namespaces really is in trying to gracefully support them in an API.
How do you do it with CMarkup? Don't do anything! Just treat all the tag names and attributes literally and you can access, create and modify any document format at all. CMarkup holds to the tenet that if you cannot greatly simplify it, don't even try. In MSXML you cannot set the
I am able to reproduce what you are observing. Any element I add under CMarkupMSXML xml;
xml.SetDefaultNamespace( _T("http://www.mimosa.org/TechXMLV3-0") );
xml.AddElem( _T("mim_0003") );
xml.AddChildElem( _T("connect_req") );
Essentially, what this does in MarkupMSXML.cpp is replace: MSXMLNS::IXMLDOMElementPtr pNew = m_pDOMDoc->createElement( _bstr_t(szName) ); with: MSXMLNS::IXMLDOMElementPtr pNew;
if ( m_strDefaultNamespace.IsEmpty() )
pNew = m_pDOMDoc->createElement( _bstr_t(szName) );
else
pNew = m_pDOMDoc->createNode(
_variant_t((short)MSXMLNS::NODE_ELEMENT),
_bstr_t(szName), _bstr_t(m_strDefaultNamespace) );
MSXML tracks namespaces to ensure correctness, but in doing so creates extra work for the developer. The
You will have to adjust your tag name logic. For example, if the root element is xml.ResetPos();
xml.FindElem();
CString csRootTag = xml.GetTagName();
CString csPre; // namespace prefix
int nColon = csRootTag.Find(':');
if ( nColon > 0 )
csPre = csRootTag.Left( nColon+1 );
then later on you can do tag names like xml.FindElem( csPre+"UserText" ); xml.FindElem( csPre+"AuthenticatedPublicType" ); | ||||||||||
|
Posted July 29, 2006. Question or comment about this article? ©Copyright 2008 First Objective Software, Inc. All rights reserved. |