Gets the XPath or XSLPattern expression string.
strExpression = objXMLDOMSelection.expr
HRESULT get_expr(BSTR* expression);
E_FAIL and formatted error message via IErrorInfo if expression is invalid.
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.
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>"
Puts the XPath or XSLPattern expression string.
objXMLDOMSelection.expr = strExpression
HRESULT put_expr(BSTR expression);
S_OK if method is successful.
E_FAIL and formatted error message via IErrorInfo if expression is invalid.
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.
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.