Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2020-11

XPath expression with Namespaces

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Simon Ameis

Hello all,

could somebody help me with using XML namespaces in an XPath expression?

I constantly get an EDOMNamespace exception with message 'TXPathScanner.ParseStep', even if I provide an TXPathNSResolver instance. The exception only disappears if I use an own TXPathNSResolver descendant class which resolves the namespace prefix 'D' to any URI. However, then the resultset is still empty. The Wiki page about xpath doesn't mention Project1;
{$mode objfpc}{$H+}

uses
  SysUtils, Classes,
  Laz2_DOM, laz2_XMLRead, laz2_xpath;

// example document
const
  XML = '<?xml version="1.0" encoding="utf-8"?>'
+'<D:multistatus xmlns:D="DAV:">'
+'<D:response xmlns:lp2="http://apache.org/dav/props/" xmlns:lp4="http://calendarserver.org/ns/"
xmlns:lp3="urn:ietf:params:xml:ns:caldav" xmlns:lp1="DAV:">' +'<D:href>/</D:href>'
+'</D:response>'
+'</D:multistatus>';

var
  s: TStringStream = nil;
  doc: TXMLDocument = nil;
  ResponseList: TXPathVariable = nil;
  ResponseNode: TDOMElement;
  Resolver: TXPathNSResolver = nil;
begin
  try
    s := TStringStream.Create(XML);
    ReadXMLFile(doc, s);
    Resolver := TXPathNSResolver.Create(doc.DocumentElement);
    // this call raises EDOMNamespace exception with message
'TXPathScanner.ParseStep'
    ResponseList := EvaluateXPathExpression('//D:Response',
doc.DocumentElement, Resolver);
    for Pointer(ResponseNode) in ResponseList.AsNodeSet do
      WriteLn(ResponseNode.TextContent);
  finally
    FreeAndNil(ResponseList);
    FreeAndNil(Resolver);
    FreeAndNil(doc);
    FreeAndNil(s);
  end;
  WriteLn('End');
  readLn;
end.

Reply by email