XPath and TXmlDocument
Asked Answered
C

1

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.

Cruces answered 21/3, 2011 at 21:25 Comment(1)
+1 for a good question; it turned out to be harder than I expected to find an answer.Rebbecarebbecca
R
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.

Rebbecarebbecca answered 21/3, 2011 at 21:54 Comment(6)
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. ;)Cruces
@Salvador: Ok, if you insist. :)Rebbecarebbecca
So how do we set the Search namespaces? ie, setProperty('SelectionNamespaces', SearchNS);Alexisaley
@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).Rebbecarebbecca
NOTE: Asked new question on that topic: #30688119Cerise
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.Pelham

© 2022 - 2024 — McMap. All rights reserved.