By Salvador


2011-03-21 21:25:10 8 Comments

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.

1 comments

@Ken White 2011-03-21 21:54:13

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.

@Salvador 2011-03-21 22:04:11

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 2011-03-21 22:21:55

@Salvador: Ok, if you insist. :)

@Robbie Matthews 2012-10-05 00:50:43

So how do we set the Search namespaces? ie, setProperty('SelectionNamespaces', SearchNS);

@Ken White 2012-10-05 01:02:00

@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 2015-06-06 21:13:43

NOTE: Asked new question on that topic: stackoverflow.com/questions/30687619/…

@Arioch 'The 2016-09-26 14:27:15

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 Questions

Sponsored Content

7 Answered Questions

[SOLVED] Getting attribute using XPath

  • 2010-12-25 22:47:41
  • GurdeepS
  • 344929 View
  • 329 Score
  • 7 Answer
  • Tags:   xml xpath

1 Answered Questions

[SOLVED] Delphi - Can TXMLDocument be configured to ignore incorrect DTD entities?

2 Answered Questions

[SOLVED] DOM Level 3 XPath in Internet Explorer

3 Answered Questions

[SOLVED] Alternative to TXMLDocument?

3 Answered Questions

[SOLVED] How to know if a node is a closing tag </> in XML using xPath?

  • 2015-05-05 09:24:22
  • Zooly
  • 1236 View
  • 2 Score
  • 3 Answer
  • Tags:   php xml xpath

3 Answered Questions

[SOLVED] C++ Builder XE2, TXMLDocument 'DTD is prohibited'

5 Answered Questions

[SOLVED] What is the fastest XML Parser available for Delphi?

2 Answered Questions

[SOLVED] Delphi Classes to XML representation:

Sponsored Content