expr Property
Gets or sets the XML Path Language (XPath) expression.
Script Syntax
strExpression = objXMLDOMSelection.expr;
objXMLDOMSelection.expr = strExpression;
Example
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var selection;
xmlDoc.loadXML("<Customer><Name>Microsoft</Name></Customer>");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
WScript.Echo("You have error " + myErr.reason);
} else {
xmlDoc.setProperty("SelectionLanguage", "XPath");
selection = xmlDoc.selectNodes("Customer/Name");
WScript.Echo(selection.expr + " -- " + selection.item(0).xml);
selection.expr = "/Customer";
WScript.Echo(selection.expr + " -- " + selection.item(0).xml);
}
Visual Basic Syntax
strExpression = objXMLDOMSelection.expr
objXMLDOMSelection.expr = strExpression
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 "msxml3.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("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;
}
sample.xml
The C++ example uses the following XML resource 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>
Output
The C++ example outputs two message boxes.
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.
Versioning
Implemented in:
MSXML 3.0, MSXML 6.0