XPath and TXmlDocument

10,320

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.

Share:
10,320

Related videos on Youtube

Salvador
Author by

Salvador

Updated on August 17, 2020

Comments

  • Salvador
    Salvador over 3 years

    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.

    • Ken White
      Ken White about 13 years
      +1 for a good question; it turned out to be harder than I expected to find an answer.
  • Salvador
    Salvador about 13 years
    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. ;)
  • Ken White
    Ken White about 13 years
    @Salvador: Ok, if you insist. :)
  • Robbie Matthews
    Robbie Matthews over 11 years
    So how do we set the Search namespaces? ie, setProperty('SelectionNamespaces', SearchNS);
  • Ken White
    Ken White over 11 years
    @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).
  • Jerry Dodge
    Jerry Dodge almost 9 years
    NOTE: Asked new question on that topic: stackoverflow.com/questions/30687619/…
  • Arioch 'The
    Arioch 'The over 7 years
    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.

Related