Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MSXML3 namespace prefix XPATH selectSingleNode

0 views
Skip to first unread message

Paul Bacelar

unread,
May 22, 2003, 4:09:31 PM5/22/03
to
My problem is:
I receive a document XML with namespaces.
I want to use the method "selectSingleNode" of
IXMLDOMDocument2 interface to recover a precise node.
All functions well except that the way that I must use,
apparently, a path with the prefixes defined in the
document and not the namespaces.
Example:
<?xml version="1.0 "? >
< a:multistatus xmlns:"DAV: ">
< a:response >
< a:propstat/>
</a:response >
</a:multistatus >
selectSingleNode(_bstr_t
(L"a:multistatus/a:response/a:propstat")) is OK
selectSingleNode(_bstr_t
(L"DAV:multistatus/DAV:response/DAV:propstat")) is not OK.
In short, how I can recover the prefix used for a given
namespace.

Thanks for your attention.

Paul Bacelar.

PS: For information, the xml document is the result of a
XMLHTTP WEBDAV request and it's our dear Exchange2000
which choose the prefixes.

Samer Boshra [VCPP MSFT]

unread,
Jun 18, 2003, 3:35:53 PM6/18/03
to
>From: "Paul Bacelar" <pbac...@net2s.comNOSPAM>
>Subject: MSXML3 namespace prefix XPATH selectSingleNode

>My problem is:
>I receive a document XML with namespaces.
>I want to use the method "selectSingleNode" of
>IXMLDOMDocument2 interface to recover a precise node.
>All functions well except that the way that I must use,
>apparently, a path with the prefixes defined in the
>document and not the namespaces.
>Example:
><?xml version="1.0 "? >
> < a:multistatus xmlns:"DAV: ">
> < a:response >
> < a:propstat/>
> </a:response >
> </a:multistatus >
>selectSingleNode(_bstr_t
>(L"a:multistatus/a:response/a:propstat")) is OK
>selectSingleNode(_bstr_t
>(L"DAV:multistatus/DAV:response/DAV:propstat")) is not OK.
>In short, how I can recover the prefix used for a given
>namespace.
>

Here is a suggestion:

// retrieve root element of the document
IXMLDOMElement* documentElement = NULL;
hr = xmlDoc->get_documentElement(&documentElement);

// retrieve attributes of the root element
IXMLDOMNamedNodeMap* nodeMap = NULL;
hr = xmlDoc->get_attributes(&nodeMap);

// iterate through all attributes
long length = 0;
hr = xmlDoc->get_length(&length);
for (long i = 0; i < length; i++) {
IXMLDOMNode* currentAttribute = NULL;
hr = nodeMap->get_item(i, &currentAttribute);
// retrieve currentAttribute prefix
BSTR prefix = NULL
hr = currentAttribute->get_prefix(&prefix);
// retrieve attribute base name
BSTR baseName = NULL
hr = currentAttribute->get_baseName(&baseName);
// retrieve attribute text
BSTR nodeText = NULL
hr = currentAttribute->get_text(&nodeText);
// if prefix == "xmlns" and nodeText == <your_namespace>, then the prefix you're looking for is the attribute baseName
// you can also create a lookup table to map namespace to prefix.
}

--
Samer Boshra, Microsoft Visual C++ Team
This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

0 new messages