context Property1

 

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

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.6.0");  
var selection;  
xmlDoc.loadXML("<Customer>Microsoft</Customer>");  
xmlDoc.setProperty("SelectionLanguage", "XPath");  
selection = xmlDoc.selectNodes("//Customer");  
WScript.Echo(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;
   WScript.Echo("You have error " + myErr.reason);
} else {
   WScript.Echo(selection.context.xml);
}

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

Visual Basic Syntax

Set objXMLDOMNode = objXMLDOMSelection.context  
objXMLDOMSelection.context = objXMLDOMNode  

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_contextonly)
The value returned if the context is invalid.

E_INVALIDARG (for get_contextonly)
The value returned if the input argument is Null.

E_FAIL (for putref_contextonly)
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 "msxml6.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

The C\C++ example uses the following XML data file.

<?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>

The C\C++ example outputs the following 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.

Versioning

Implemented in:

MSXML 3.0, MSXML 6.0

Applies to

IXMLDOMSelection