none
Dynamic namespace prefix issue in xpath of the xml tag - MSXML

    Question

  • Hi,

    I am trying to access my xml using "MSXML2' dom object  using vb scripting.

    Set oMessageDoc = CreateObject( "MSXML2.DOMDocument" )

    however I have dynamic namespace prefix for same tag in each xml file. Example provided below

    Tag-> <ns1:Addr><ns1:Street>abc street </ns1:Street> </ns1:Addr>

    same tag is referred in another xml as, <ns2:Addr><ns2:Street>abc street </ns2:Street> </ns2:Addr>

    Currently,

    I am trying to access xml tag as "ns1:Addr/ns1:Street" and  "ns2:Addr/ns2:Street"  though both refers same tag (ns1, ns2 are same namespace URI)

    is it possible to access xmltag's xpath without prefix, as like "Addr/Street"?


    Wednesday, May 08, 2013 9:48 PM

All replies

  • First of all, with "MSXML2.DOMDocument" you are getting MSXML 3.0 for which the selection language has to be explicitly set to XPath with e.g.

      oMessageDoc.setProperty "SelectionLanguage", "XPath"

    As for using "Addr/Street", no, with XPath 1.0 which is all Microsoft supports that always selects elements of those names in no namespace.

    But of course if the elements are in the same namespace then all you need is

      oMessageDoc.setProperty "SelectionNamespaces", "xmlns:ns1='http://example.com/ns'"

    (where you put the namespace URI of the input XML in the place of http://example.com/ns) and then in your code you can select

      oMessageDoc.selectNodes("//ns1:Addr/ns1:Street")

    or

      oMessageDoc.selectSingleNode("(//ns1:Addr/ns1:Street)[2]")


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    Thursday, May 09, 2013 10:07 AM