1

I'm an XML newbie and I have an XML document (which I can't edit because it comes from somewhere else) but it has a root node like this:

<Configuration xmlns="http://schemas.mycomp.com/product/settings" version="2.0.0">

I'm trying to parse this document with msxml and xpath and I've done it successfully if I remove the xmlns attribute. For some reason, with this xmlns attribute in place, the document won't parse. I've attempted to set the msxml parse to recognise the document using:

m_pXMLDoc->setProperty( _bstr_t(L"AllowDocumentFunction"), _variant_t(true));   
m_pXMLDoc->setProperty( _bstr_t(L"AllowXsltScript"), _variant_t(true));
m_pXMLDoc->setProperty( _bstr_t(L"SelectionLanguage"), _variant_t(L"XPath"));   
m_pXMLDoc->setProperty( _bstr_t(L"SelectionNamespaces"), _variant_t(L"xmlns='http://schemas.mycomp.com/product/settings'"));

m_pXMLDoc->preserveWhiteSpace = VARIANT_FALSE;
m_pXMLDoc->resolveExternals = VARIANT_TRUE; 
m_pXMLDoc->validateOnParse = VARIANT_FALSE;

From reading around it looks like xpath only works on the "no name" namespace and this document sets the default namespace so that it's no longer "no name". Can I set the namespace that xpath uses using MSXML?

2
  • Hmm, I've noticed that if I edit the XML file and change the xmlns attribute to xmlns:x="schemas.mycomp.com/product/settings" then the document parses just fine. I guess it's something to do with the elements being in the default namespace that's causing me problems... Commented Jan 11, 2012 at 11:22
  • Don't forget to include x: in your XPath expression in this case. /x:Element will still match <Element xmlns="schemas.mycomp.com/product/settings">, while /Element will match two things: <Element> with a default namespace and <Element> with no namespace. Commented Jul 3, 2013 at 11:41

1 Answer 1

2

From Microsoft : This behaviour is by design ...

See http://support.microsoft.com/kb/288147

Use prefixes with the namespaces when you specify the SelectionNamespaces property

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I feared this was the case. Sadly not a great answer if you need to parse someone elses XML though :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.