19

In Delphi XE is it possible to use XPath with a TXmlDocument component?

I'm aware I can use late binding to access the MSXML2 and then use XPath:

XML := CreateOleObject('MSXML2.DOMDocument.3.0') ;
XML.async := false;
XML.SetProperty('SelectionLanguage','XPath');

But I wanna know if TXmlDocument installed with Delphi XE supports XPath.

bluish
  • 26,356
  • 27
  • 122
  • 180
Salvador
  • 16,132
  • 33
  • 143
  • 245

1 Answers1

15

I can't find anything in the TXMLDocument documentation about XPath.

XML example, from the OmniXML XPath demo:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book>
    <title lang="eng">Harry Potter</title>
  </book>
  <book>
    <title lang="eng">Learning XML</title>
  </book>
  <book>
    <title lang="slo">Z OmniXML v lepso prihodnost</title>
    <year>2006</year>
  </book>
  <book>
    <title>Kwe sona standwa sam</title>
  </book>
</bookstore>

Try something like this:

uses 
  XMLDoc, XMLDom, XMLIntf;

// From a post in Embarcadero's Delphi XML forum.
function selectNode(xnRoot: IXmlNode; const nodePath: WideString): IXmlNode;
var
  intfSelect : IDomNodeSelect;
  dnResult : IDomNode;
  intfDocAccess : IXmlDocumentAccess;
  doc: TXmlDocument;
begin
  Result := nil;
  if not Assigned(xnRoot) or not Supports(xnRoot.DOMNode, IDomNodeSelect, intfSelect) then
    Exit;
  dnResult := intfSelect.selectNode(nodePath);
  if Assigned(dnResult) then
  begin
    if Supports(xnRoot.OwnerDocument, IXmlDocumentAccess, intfDocAccess) then
      doc := intfDocAccess.DocumentObject
    else
      doc := nil;
    Result := TXmlNode.Create(dnResult, nil, doc);
  end;
end;


var
  IDoc: IXMLDocument;
  INode: IXMLNode;
begin
  IDoc := LoadXMLDocument('.\books.xml');
  INode := SelectNode(IDoc.DocumentElement,'/bookstore/book[2]/title'); 
end;

Just as an FYI for others, I'll leave this in: OmniXML supports XPath, and has a demo that shows really well how to use it. It's also free, comes with source, supports Unicode, and has pretty good support through it's forums.

bluish
  • 26,356
  • 27
  • 122
  • 180
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Ken, many thnaks for your suggestion, but for now, I just want to know if is possible to use `TXMLDocument ` with `XPath`, avoiding the use of the third party components. ;) – Salvador Mar 21 '11 at 22:04
  • @Salvador: Ok, if you insist. :) – Ken White Mar 21 '11 at 22:21
  • So how do we set the Search namespaces? ie, setProperty('SelectionNamespaces', SearchNS); – Robbie Matthews Oct 05 '12 at 00:50
  • @Robbie, I'm not sure. I haven't needed to do that, I'm afraid. You should post this as a new question of your own; you can always reference this answer as background (using the link available by right-clicking `share` below the answer and copying the link location). – Ken White Oct 05 '12 at 01:02
  • NOTE: Asked new question on that topic: http://stackoverflow.com/questions/30687619/how-to-use-xpath-on-txmldocument-while-using-namespaces – Jerry Dodge Jun 06 '15 at 21:13
  • OmniXML has VERY limited support for XPath. Even to have simple `X<>Y` condition I had to patch the library. Kluug's OXML site has a speed shootout of some different XML libs for Delphi, so might be a starting point to explore alternatives. – Arioch 'The Sep 26 '16 at 14:27