none
Select nodes using getElementsByTagName method and Namespaces RRS feed

  • Question

  • Good morning/afternoon,

    My application uses XML to store information about Forms and Report; controls, RecordSource and such. Late bound to MSXML2.DOMDocument.6.0 and MSXML2.IXMLDOMNodeList to get the list.

    I need to retrieve all RecordSource nodes to process them. I have a method that does that, as a matter of fact I am currently using the getElementsByTagName XML Method, but I without the xmlns="http://something.ca/maintenance"

    This is a simplified version of my XML:

    <?xml version="1.0" encoding="utf-8"?>

    <Objects xmlns="http://something.ca/maintenance">

      <Forms>

        <Form name="frmCustomerReviewConcerns" datecreated="11/18/2014 5:24:26 PM">

          <RecordSource><![CDATA[SELECT bla,bla, bla FROM tblBla]]></RecordSource>

          <Controls>

            <Control name="cmbConcernResolution">

              <type>111</type>

              <ControlType>ComboBox</ControlType>

              <StatusBarText></StatusBarText>

              <ControlTipText></ControlTipText>

              <RowSource RowSourceType="Table/Query">

                <![CDATA[SELECT bla, blabla ORDER BY bla;]]>

              </RowSource>

            </Control>

            <Control name="Label49">

              <type>100</type>

              <ControlType>Label</ControlType>

              <Caption>Resolution:</Caption>

            </Control>

          </Controls>

        </Form>

        <Form name="frmFloatingLogin" datecreated="11/19/2014 8:02:48 AM">

          <RecordSource></RecordSource>

          <Controls>

            <Control name="txtPassword">

              <type>109</type>

              <ControlType>TextBox</ControlType>

              <StatusBarText></StatusBarText>

              <ControlTipText></ControlTipText>

              <ControlSource></ControlSource>

            </Control>

            <Control name="cmbUser">

              <type>111</type>

              <ControlType>ComboBox</ControlType>

              <StatusBarText></StatusBarText>

              <ControlTipText></ControlTipText>

              <RowSource RowSourceType="Table/Query">

                <![CDATA[SELECT bla FROM tblBla;]]>

              </RowSource>

            </Control>

          </Controls>

        </Form>

      </Forms>

    </Objects>

    My question is:  How do I access this information using the NameSpaces?. NOTE:I am already declaring the SelectionNamespaces: oXMLDoc.SetProperty "SelectionNamespaces", "xmlns:xsl=' http://something.ca/maintenance'".

    It does not return anything. I would appreciate any help. Have a blessed day!


    Gustavo Miller

    Thursday, March 23, 2017 1:21 PM

Answers

  • Thank you Celeste!

    I think I have already settle with 'removing the xmlns namespace' and use the getElementsByTagName method -it returns what I need. It does the trick and I can get things done. Unfortunately I am not using any of the .NET Frameworks as this is using Access VBA (VB6) so I am limited.

    I use the conventional methods such as SelectNodes/SelectSingleNode for other purposes.

    I will review the link you have sent me; as a contractor I need to get things done, not much space for research and fiddling around with code.

    Thank you for your time...


    Gustavo Miller

    Monday, March 27, 2017 1:05 PM

All replies

  • Hi Gustavo,

    We could use selectSingleNode Method or selectNodes Method.

    E.g.

    xmlDoc.SetProperty "SelectionNamespaces", "xmlns:xsl=' http://something.ca/maintenance'"
    
    Set node = xmlDoc.SelectSingleNode("Objects/Forms/Form/RecordSource")
    Debug.Print node.xml
    
    Set nodes = xmlDoc.SelectNodes("Objects/Forms/Form/RecordSource")
    For Each node In nodes
    Debug.Print node.xml
    Next

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 24, 2017 3:56 AM
    Moderator
  • Thank you for your input Celeste, I appreciate you taking the time to reply.

    Yes, definitely that is one way to access information, but I was wondering why the getElementsByTagName does not work when NameSpaces are define in the XML. As soon as you remove the NameSpace the getElementsByTagName  returns the NodesList; just as documented in MSDN.

    In the code you have sent as an example I am assuming that you have excluded the use of the SelectionNamespaces - I guess to keep it simple, but just to clarify...

    xmlDoc.SetProperty "SelectionNamespaces" "xmlns:xsl='http://something.ca/maintenance'"

    Set node = xmlDoc.SelectSingleNode("//xsl:Objects/xsl:Forms/xsl:Form/xsl:RecordSource").

    Set nodes = xmlDoc.SelectNodes("//xsl:Objects/xsl:Forms/xsl:Form/xsl:RecordSource").

    Thank you for you help and happy coding!


    Gustavo Miller

    Friday, March 24, 2017 3:30 PM
  • Hello,

    It seems that I misunderstood your question. Do you want to know why getElementsByTagName doesn't work for XPath like "//xsl:Objects/xsl:Forms/xsl:Form/xsl:RecordSource" ?

    Because getElementsByTagName selects nodes by tag name instead of XPath. "RecordSource" is its tag name.

    You may visit the link below to see the different between SelectNodes and getElementsByTagName:

    http://stackoverflow.com/questions/2515097/selectnodes-and-getelementsbytagname

    If you want to use XPath, please use SelectNodes/SelectSingleNode. If you want to use XPath with namespace prefix, you could visit How to use XPath to query against a user-defined default namespace. In my previous post, add line: xmlDoc.setProperty "SelectionLanguage", "XPath" before setting namespace, then we could use XPath with namespace prefix like "//xsl:Objects/xsl:Forms/xsl:Form/xsl:RecordSource" to return nodes.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 27, 2017 6:59 AM
    Moderator
  • Thank you Celeste!

    I think I have already settle with 'removing the xmlns namespace' and use the getElementsByTagName method -it returns what I need. It does the trick and I can get things done. Unfortunately I am not using any of the .NET Frameworks as this is using Access VBA (VB6) so I am limited.

    I use the conventional methods such as SelectNodes/SelectSingleNode for other purposes.

    I will review the link you have sent me; as a contractor I need to get things done, not much space for research and fiddling around with code.

    Thank you for your time...


    Gustavo Miller

    Monday, March 27, 2017 1:05 PM