Microsoft XML SDK 2.6 - XML Reference

IXMLDOMSelection::expr Method

IXMLDOMSelection::get_expr Method

Gets the XPath or XSLPattern expression string.

Visual Basic Syntax

strExpression = objXMLDOMSelection.expr

C/C++ Syntax

HRESULT get_expr(BSTR* expression);

Parameters

expr [out]
The IXMLDOMSelection expression string is either XSLPattern or XPath syntax, depending on the setting of the SelectionLanguage property on the IXMLDOMDocument2 interface that created this Selection object. (For details, see IXMLDOMDocument2::setProperty.) Once created, the XMLDOMSelection object continues to use the expression language it was created with.

C/C++ Return Values

S_OK
Value returned if method is successful.

E_FAIL and formatted error message via IErrorInfo if expression is invalid.

Remarks

Setting a new expression immediately resets the state of this node list to the beginning unless the expression is invalid and an error is returned, in which case it has no effect. It does not reset the context.

Example

Dim xmlDoc As New MSXML2.DOMDocument
Dim selection As MSXML2.IXMLDOMSelection

xmlDoc.loadXML ("<Customer><Name>Microsoft</Name></Customer>")
xmlDoc.setProperty "SelectionLanguage", "XPath"
Set selection = xmlDoc.selectNodes("Customer/Name")
MsgBox selection.expr + " -- " + selection.Item(0).xml

Displays "Customer/Name -- <Name> Microsoft</Name>"

selection.expr = "/Customer"

The selection is immediately refreshed with a list corresponding to the new expression

MsgBox selection.expr + " -- " + selection.Item(0).xml

Displays "/Customer -- <Customer><Name> Microsoft</Name></Customer>"



IXMLDOMSelection::put_expr Method

Puts the XPath or XSLPattern expression string.

Visual Basic Syntax

objXMLDOMSelection.expr = strExpression

C/C++ Syntax

HRESULT put_expr(BSTR expression);

Parameters

expr [in]
The IXMLDOMSelection expression string is either XSLPattern or XPath syntax, depending on the setting of the SelectionLanguage property on the IXMLDOMDocument2 interface that created this Selection object. (For details, see IXMLDOMDocument2::setProperty). Once created, the XMLDOMSelection object continues to use the expression language it was created with.

C/C++ Return Values

S_OK if method is successful.

E_FAIL and formatted error message via IErrorInfo if expression is invalid.

Remarks

Setting a new expression immediately resets the state of this node list to the beginning unless the expression is invalid and an error is returned, in which case it has no effect. It does not reset the context.

Example

The following code example demonstrates the resetting of the selected nodes list if the expr property is changed:

BOOL DOMSelectionExprDemo()
{
   BOOL bResult = FALSE;
   short sResult = FALSE;
   long lvalue;
   IXMLDOMSelection *pIXMLDOMSelection=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\\sample.xml")), &sResult);
            if(SUCCEEDED(hr) && (sResult==VARIANT_TRUE))
            {
               hr=pIXMLDOMDocument2->selectNodes( 
                  _T("*/BOOK[TITLE='Cosmos']"), 
                  (IXMLDOMNodeList**)&pIXMLDOMSelection);
               if(SUCCEEDED(hr))
               {
                  if(SUCCEEDED(hr) && pIXMLDOMSelection)
                  {
                     hr = pIXMLDOMSelection->get_expr(&bstrValue);
                     if(SUCCEEDED(hr))
                     {
                        ::MessageBox(NULL, bstrValue, _T("Current 
                           Expression"), MB_OK);
                        bResult=TRUE;
                        hr = pIXMLDOMSelection->get_length(&lvalue);
                        hr = pIXMLDOMSelection->put_expr(_T("*/BOOK"));
                        hr = pIXMLDOMSelection->get_length(&lvalue);
                     }
                     RELEASE(pIXMLDOMSelection);
                  }
               }
            }
         }
         RELEASE(pIXMLDOMDocument2);
      }
   }
   catch(...)
   {
      CHECK_AND_RELEASE(pIXMLDOMSelection);
      CHECK_AND_RELEASE(pIXMLDOMDocument2);
      DisplayErrorToUser();
   }
   return bResult;
}

d:\\inetpub\\wwwroot\\sample.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>Cosmos</TITLE>
      <AUTHOR>Carl Sagan</AUTHOR>
      <PUBLISHER>Ballantine Books</PUBLISHER>
   </BOOK>
   <BOOK>
      <TITLE>Catwings</TITLE>
      <AUTHOR>Ursula K. Le Guin</AUTHOR>
      <PUBLISHER>Scholastic</PUBLISHER>
   </BOOK>
</COLLECTION>

Output

The first messagebox would display "1".

The second would display "2". When the expression changes, the nodes in the selection are recomputed using the new expression. No changes are made to the context property.

See Also

IXMLDOMSelection Interface