// Originale Quelle: Zarko Gajic:
// http://delphi.about.com/od/vclusing/qt/delphi-select-xml-nodes-ixmlnodelist-selectnodes-xpath-xmldom.htm, 18.06.2013
// Erweitert um Rekursive Parents.
function XPathQuery(aNode: IXMLNode; aQuery:
string): IXMLNodeList;
overload;
var
XmlNodeAccess: IXmlNodeAccess;
XmlDocAccess: IXmlDocumentAccess;
XmlDomNodeSelect: IDomNodeSelect;
DomNodeList: IDomNodeList;
Document: TXMLDocument;
i: integer;
OwnerDoc: TXMLDocument;
DomDoc2: IXMLDOMDocument2;
function CreateWithParentNode(
const aDomNode: IDOMNode;
const aOwnerDoc: TXMLDocument): TXmlNode;
begin
if assigned(aDomNode)
then
Result := TXMLNode.Create(aDomNode, CreateWithParentNode(aDomNode.parentNode, aOwnerDoc), aOwnerDoc)
else
Result :=
nil;
end;
begin
Result :=
nil;
if not assigned(aNode)
then
Exit;
if not Supports(aNode, IXmlNodeAccess, XmlNodeAccess)
then
raise Exception.Create('
Interface IXmlNodeAccess not found.');
if not Supports(aNode.DOMNode, IDomNodeSelect, XmlDomNodeSelect)
then
raise Exception.Create('
Interface IDomNodeSelect not found.');
if Supports(aNode.OwnerDocument, IXmlDocumentAccess, XmlDocAccess)
then
OwnerDoc := XmlDocAccess.DocumentObject
else
OwnerDoc :=
nil;
// if Owner is nil this is a possble Memory Leak!
//>>> if XPath is not enabled
if assigned(OwnerDoc)
then
if Supports(OwnerDoc.DOMDocument, IXMLDOMDocument2, DomDoc2)
then
DomDoc2.setProperty('
SelectionLanguage', '
XPath');
//<<< if XPath is not enabled
DomNodeList := XmlDomNodeSelect.selectNodes(aQuery);
if assigned(DomNodeList)
then
begin
Result := TXMLNodeList.Create(XmlNodeAccess.GetNodeObject, '
',
nil);
Document := OwnerDoc;
for i := 0
to pred(DomNodeList.length)
do
begin
Result.Add(CreateWithParentNode(DomNodeList.item[i], Document));
end;
end
end;