Discussion:
Using a default namespace with Xpath in VBScript.
(too old to reply)
Paul Le Sueur
2003-11-11 12:58:34 UTC
Permalink
Dear All,

I have been trying to use XPath with the DOMDocument object and selectnodes
function, to query an XML document containing a default namespace. I have
seen other postings around this subject but have still not found one which
answers my specific issues.

When I try and query an XML document which does not contain a default
namespace I get no problems, the nodes I require are returned by the
selectnodes function.
When, however, I try and query an XML document with a default namespace, the
selectnodes function will not return anything.

I gather this problem is down to the need to specify a namespace against the
DOMdocument object when loading my XML document. While there are plenty of
resources specifying how to do this with VB.Net, I can't find any references
that tell me how to do this with VB6 or VBScript (I need to use these as I am
writing code for a Microsoft InfoPath form, which uses script)

Any advice would be appreciated!

Thank you,

Paul.
Joe Fawcett
2003-11-11 14:09:35 UTC
Permalink
Post by Paul Le Sueur
Dear All,
I have been trying to use XPath with the DOMDocument object and selectnodes
function, to query an XML document containing a default namespace. I have
seen other postings around this subject but have still not found one which
answers my specific issues.
When I try and query an XML document which does not contain a default
namespace I get no problems, the nodes I require are returned by the
selectnodes function.
When, however, I try and query an XML document with a default namespace, the
selectnodes function will not return anything.
I gather this problem is down to the need to specify a namespace against the
DOMdocument object when loading my XML document. While there are plenty of
resources specifying how to do this with VB.Net, I can't find any references
that tell me how to do this with VB6 or VBScript (I need to use these as I am
writing code for a Microsoft InfoPath form, which uses script)
Any advice would be appreciated!
Thank you,
Paul.
Althogh I don't know for certain which version parser InfoPath uses you
almost certainly need to use the setProperty method against the DomDocument:
XDocument.DOM.setProperty "SelectionNamespaces",
"xmlns:default='http://namespace'"
Replace 'http://namespace' with the namespace uri that you are interested
in, you can replace the word 'default' if you wish as well with your own
choice. Then select like this:
Set colNodes = XDocument.DOM.selectNodes("/default:data/item")
for a document like:
<data xmlns="http://namespace">
<item>Item 1</item>
<item>Item 2</item>
</data>

Joe (MVP - xml)
Ricky
2003-11-13 21:11:41 UTC
Permalink
in VB6 the code would look something like...

Dim xmlDoc As New MSXML2.DOMDocument40
Dim xNodes As IXMLDOMNodeList

If xmlDoc.Load(file.Path) Then
xmlDoc.setProperty xmlDoc.setProperty "SelectionNamespaces", _
"xmlns:x='http://your.namespace.com'"
Set xNodes = xmlDoc.selectNodes("Path:x/To/Element")
Else
'error code blah blah
End If

...

worked like a charm for me

ricky
Post by Paul Le Sueur
Dear All,
I have been trying to use XPath with the DOMDocument object and selectnodes
function, to query an XML document containing a default namespace. I have
seen other postings around this subject but have still not found one which
answers my specific issues.
When I try and query an XML document which does not contain a default
namespace I get no problems, the nodes I require are returned by the
selectnodes function.
When, however, I try and query an XML document with a default namespace, the
selectnodes function will not return anything.
I gather this problem is down to the need to specify a namespace against the
DOMdocument object when loading my XML document. While there are plenty of
resources specifying how to do this with VB.Net, I can't find any references
that tell me how to do this with VB6 or VBScript (I need to use these as I am
writing code for a Microsoft InfoPath form, which uses script)
Any advice would be appreciated!
Thank you,
Paul.
Loading...