context Property

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

context Property

Gets the node (subtree) that is applied to the selection.

[Script]

Script Syntax

var objXMLDOMNode = objXMLDOMSelection.context;
objXMLDOMSelection.context = objXMLDOMNode;

Example

The following script example shows what is contained in the context of a selection after a query is executed. It also shows that the selection is reset when this property is changed.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var selection;
xmlDoc.loadXML("<Customer>Microsoft</Customer>");
xmlDoc.setProperty("SelectionLanguage", "XPath");
selection = xmlDoc.selectNodes("//Customer");
alert(selection.context.xml);

The selection.context.xml contains "<Customer>Microsoft</Customer>"; selection.length = 1.

xmlDoc.loadXML("<new>none</new>");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   alert(selection.context.xml);
}

The selection.context.xml contains "<new>none</new>"; selection.length = 0.

[Visual Basic]

Visual Basic Syntax

Set objXMLDOMNode = objXMLDOMSelection.context
objXMLDOMSelection.context = objXMLDOMNode

Example

The following Microsoft® Visual Basic® example shows what is contained in the context of a selection after a query is executed. It also shows that the selected set is reset when this property is changed.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim selection As Msxml2.IXMLDOMSelection

xmlDoc.loadXML ("<Customer>Microsoft</Customer>")
xmlDoc.setProperty "SelectionLanguage", "XPath"
Set selection = xmlDoc.selectNodes("//Customer")
MsgBox selection.context.xml

The selection.context.xml contains "<Customer>Microsoft</Customer>"; selection.length = 1.

xmlDoc.loadXML ("<new>none</new>")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   MsgBox selection.context.xml
End If

The selection.context.xml contains "<new>none</new>"; selection.length = 0.

[C/C++]

C/C++ Syntax

HRESULT get_context(IXMLDOMNode ** ppNode);
HRESULT putref_context(IXMLDOMNode * pNode);

Parameters

ppNode [out, retval]
The subtree to apply the pattern to.
pNode [in]
The subtree to apply the pattern to.

C/C++ Return Values

S_OK
The value returned if the method is successful.
S_FALSE (for get_context only)
The value returned if the context is invalid.
E_INVALIDARG (for get_context only)
The value returned if the input argument is Null.
E_FAIL (for putref_context only)
The value returned if a node with a different threading model is provided.

Example

The following C/C++ example shows what is contained in the context property after selectNodes is executed.

#import "msxml5.dll"
using namespace MSXML2;

#define CHECK_AND_RELEASE(pInterface)  \
if(pInterface) \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\

#define RELEASE(pInterface)  \
   {\
pInterface->Release();\
pInterface = NULL;\
   }\

BOOL DOMSelectionContextDemo()
{
   BOOL bResult = FALSE;
   short sResult = FALSE;
   IXMLDOMSelection *pIXMLDOMSelection=NULL;
   IXMLDOMNode *pIXMLDOMNode=NULL;
   IXMLDOMDocument2 *pIXMLDOMDocument2=NULL;
   HRESULT hr;
   BSTR bstrValue;

   try
   {
      hr=CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_SERVER, 
         IID_IXMLDOMDocument2, (LPVOID*)(&pIXMLDOMDocument2));
      SUCCEEDED(hr) ? 0 : throw hr;

      if(pIXMLDOMDocument2)
      {
         hr=pIXMLDOMDocument2->put_async(VARIANT_FALSE);
         if(SUCCEEDED(hr))
         {
            hr=pIXMLDOMDocument2->load(_variant_t( 
               _T("d:\\inetpub\\wwwroot\\samplexml.xml")), &sResult);
            if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
            {
               hr=pIXMLDOMDocument2->selectNodes( 
               _T("*/BOOK[TITLE='Lover Birds']"),
               (IXMLDOMNodeList**)&pIXMLDOMSelection);
               if(SUCCEEDED(hr))
               {
                  if(SUCCEEDED(hr) && pIXMLDOMSelection)
                  {
                     hr=pIXMLDOMSelection->get_context(&pIXMLDOMNode);
                     if(SUCCEEDED(hr) && pIXMLDOMNode)
                     {
                        hr=pIXMLDOMNode->get_xml(&bstrValue);
                        if(SUCCEEDED(hr))
                           ::MessageBox(NULL, bstrValue, _T("Current 
                              Selection Context"), MB_OK);
                        bResult=TRUE;
                        RELEASE(pIXMLDOMNode);
                     }
                     RELEASE(pIXMLDOMSelection);
                  }
               }
            }
         }
         RELEASE(pIXMLDOMDocument2);
      }
   }
   catch(...)
   {
      CHECK_AND_RELEASE(pIXMLDOMNode);
      CHECK_AND_RELEASE(pIXMLDOMSelection);
      CHECK_AND_RELEASE(pIXMLDOMDocument2);
      DisplayErrorToUser();
   }
   return bResult;
}

Data File: d:\\inetpub\\wwwroot\\samplexml.xml

<?xml version='1.0'?>
<COLLECTION xmlns:dt="urn:schemas-microsoft-com:datatypes">
   <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE>
   <BOOK>
      <TITLE>Lover Birds</TITLE>
      <AUTHOR>Cynthia Randall</AUTHOR>
      <PUBLISHER>Lucerne Publishing</PUBLISHER>
   </BOOK>
   <BOOK>
      <TITLE>The Sundered Grail</TITLE>
      <AUTHOR>Eva Corets</AUTHOR>
      <PUBLISHER>Lucerne Publishing</PUBLISHER>
   </BOOK>
</COLLECTION>

Output (in a message box)

<?xml version='1.0'?>
<COLLECTION xmlns:dt="urn:schemas-microsoft-com:datatypes">
   <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE>
   <BOOK>
      <TITLE>Lover Birds</TITLE>
      <AUTHOR>Cynthia Randall</AUTHOR>
      <PUBLISHER>Lucerne Publishing</PUBLISHER>
   </BOOK>
   <BOOK>
      <TITLE>The Sundered Grail</TITLE>
      <AUTHOR>Eva Corets</AUTHOR>
      <PUBLISHER>Lucerne Publishing</PUBLISHER>
   </BOOK>
</COLLECTION>

Remarks

Because the selectNodes method can be called on any node type, context can also be assigned any node type. A node from a document different from the one that originally created IXMLDOMSelection can also be specified as long as it has the same threading model. Calling context also resets the state of the node list so that nextNode starts over.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Applies to: IXMLDOMSelection