expr Property

MSXML 5.0 SDK

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

expr Property

Gets or sets the XML Path Language (XPath) expression.

[Script]

Script Syntax

strExpression = objXMLDOMSelection.expr;
objXMLDOMSelection.expr = strExpression;

Example

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var selection;
xmlDoc.loadXML("<Customer><Name>Microsoft</Name></Customer>");
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   xmlDoc.setProperty("SelectionLanguage", "XPath");
   selection = xmlDoc.selectNodes("Customer/Name");
   alert(selection.expr + " -- " + selection.item(0).xml);

This displays "Customer/Name -- <Name> Microsoft</Name>".

   selection.expr = "/Customer";

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

   alert(selection.expr + " -- " + selection.item(0).xml);
}

This displays "/Customer -- <Customer><Name> Microsoft</Name></Customer>".

[Visual Basic]

Visual Basic Syntax

strExpression = objXMLDOMSelection.expr
objXMLDOMSelection.expr = strExpression

Example

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

xmlDoc.loadXML ("<Customer><Name>Microsoft</Name></Customer>")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   xmlDoc.setProperty "SelectionLanguage", "XPath"
   Set selection = xmlDoc.selectNodes("Customer/Name")
   MsgBox selection.expr + " -- " + selection.Item(0).xml

This 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
End If

This displays "/Customer -- <Customer><Name> Microsoft</Name></Customer>".

[C/C++]

C/C++ Syntax

HRESULT get_expr(BSTR* expression);
HRESULT put_expr(BSTR expression);

Parameters

expression [out, retval][in]
A string specifying the XPath expression to be returned or set.

C/C++ Return Values

S_OK
The value returned if method is successful.

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

Example

The following C/C++ example shows the resetting of the selected nodes list if the expr property is changed.

#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 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='Lover Birds']"),
                  (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;
}

File Name: 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>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)

The first message box will display "1".

The second message box will 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.

Remarks

Immediately setting a new expression 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 property.

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