Sign in Join

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

Mailing lists / fpc-pascal / 2013-03

fcl-xml

16 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Unknown author

Hi,
working with fcl-xml some questions came together. Every hint is appreciated.

1.
No matter what I do and which (short & valid) xml file I use - getElementById is always nil (tested with trunk and latest stable 2.6.2) and the same applies to TXMLDocument.IDs.
I tried with ReadXMLFile and TDOMParser with different options - but the result is always the same.

Is getElementById supposed to work properly or is it something that needs to be implemented?
Trying to understand what the parser does (not that easy), I got the feeling that no Hashtable for the IDs is created - can someone with more insight comment on that?

2.
Is there a way to tell WriteXMLFile *not* to use indentation?

3.
What is the state of XPath?
The readme says 'Should be fairly completed' - can I assume it works for simple tasks?

Many thanks for any hint,
d.l.i.w

Mattias Gaertner
to Unknown author
Show quoted text 15 lines
Hi, working with fcl-xml some questions came together. Every hint is appreciated. 1. No matter what I do and which (short & valid) xml file I use - getElementById is always nil (tested with trunk and latest stable 2.6.2) and the same applies to TXMLDocument.IDs. I tried with ReadXMLFile and TDOMParser with different options - but the result is always the same. Is getElementById supposed to work properly or is it something that needs to be implemented? Trying to understand what the parser does (not that easy), I got the feeling that no Hashtable for the IDs is created - can someone with more insight comment on that?

It seems to be started, but not finished.
I have not written it, just looked at the implementation.

2. Is there a way to tell WriteXMLFile *not* to use indentation?

No.

3. What is the state of XPath? The readme says 'Should be fairly completed' - can I assume it works for simple tasks?

Yes.

Mattias

Michael Van Canneyt
to Unknown author: working with fcl-xml some questions came together. Every hint is appreciated. 1. No…
Show quoted text 15 lines
Hi, working with fcl-xml some questions came together. Every hint is appreciated. 1. No matter what I do and which (short & valid) xml file I use - getElementById is always nil (tested with trunk and latest stable 2.6.2) and the same applies to TXMLDocument.IDs. I tried with ReadXMLFile and TDOMParser with different options - but the result is always the same. Is getElementById supposed to work properly or is it something that needs to be implemented? Trying to understand what the parser does (not that easy), I got the feeling that no Hashtable for the IDs is created - can someone with more insight comment on that?

It should be filled ?
The XMLTextReader creates the map, and passes it on to the final document.

2. Is there a way to tell WriteXMLFile *not* to use indentation?

No.

Show quoted text 5 lines
3. What is the state of XPath? The readme says 'Should be fairly completed' - can I assume it works for simple tasks?

This I do not know.

Michael.

Mattias Gaertner
to Michael Van Canneyt
Show quoted text 9 lines
[...] > Is getElementById supposed to work properly or is it something that needs to > be implemented? > Trying to understand what the parser does (not that easy), I got the feeling > that no Hashtable for the IDs is created - can someone with more insight > comment on that? It should be filled ? The XMLTextReader creates the map, and passes it on to the final document.

Normally getElementById should work in the DOM, not only when reading a xml document.

Mattias

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 15 lines
On Tue, 12 Mar 2013 11:52:06 +0100 (CET) Michael Van Canneyt wrote: > [...] >> Is getElementById supposed to work properly or is it something that needs to >> be implemented? >> Trying to understand what the parser does (not that easy), I got the feeling >> that no Hashtable for the IDs is created - can someone with more insight >> comment on that? > > It should be filled ? > The XMLTextReader creates the map, and passes it on to the final document. Normally getElementById should work in the DOM, not only when reading a xml document.

Correct, but since OP was using the read methods, it should work ?

Michael.

Unknown author
to Michael Van Canneyt: It should be filled ? The XMLTextReader creates the map, and passes it on to the final…

Hi,

The XMLTextReader creates the map, and passes it on to the final document.
// Trunk
procedure TLoader.ProcessXML(ADoc: TDOMDocument; AReader: TXMLTextReader);
begin
  [...]
  doc.IDs := reader.IDMap;
  reader.IDMap := nil;
end;
// 2.6.2
=== snip ===
  doc.IDs := FIDMap;
  FIDMap := nil;

==========

Just rechecked again...
The debugger clearly says, that reader.IDMap / FIDMap is nil... [Before the second assignement - of course :) ]

It seems to be started, but not finished.

If so, what needs to be done?

Regards,
d.l.i.w

Mattias Gaertner
to Unknown author
Show quoted text 7 lines
[...] Just rechecked again... The debugger clearly says, that reader.IDMap / FIDMap is nil... [Before the second assignement - of course :) ] >It seems to be started, but not finished. If so, what needs to be done?

Maybe the bug is that it checks case sensitive:

procedure TXMLTextReader.ParseAttlistDecl;
...
          Found := FSource.Matches(AttrDataTypeNames[dt]);
...
function TXMLCharSource.Matches(const arg: WideString): Boolean;
begin
  Result := False;
  if (FBufEnd >= FBuf + Length(arg)) or Reload then
    Result := CompareMem(Pointer(arg), FBuf, Length(arg)*sizeof(WideChar));
  if Result then
  begin
    Inc(FBuf, Length(arg));
    if FBuf >= FBufEnd then
      Reload;
  end;
end;

Mattias

Unknown author
to Mattias Gaertner
Show quoted text 5 lines
Maybe the bug is that it checks case sensitive: procedure TXMLTextReader.ParseAttlistDecl; ... Found := FSource.Matches(AttrDataTypeNames[dt]);

ParseAttlistDecl isn't called a single time...
- to me it sounds like "Parse Attribute *declaration*" - and I don't have a DTD (the file is actually HTML).

When switching Validate *on*, there are lot of DoError calls - but don't know if this is related...

From what I see: (trunk)

AddId is only called from ValidateAttrValue
ValidateAttrValue is only called from ValidateCurrentNode and this is only when the parser does validation...

For me there is missing something in ParseAttribute(ElDef: TElementDecl);
===
  if Assigned(ElDef) then
  begin
    AttDef := ElDef.GetAttrDef(attrName);
    // mark attribute as specified
    if Assigned(AttDef) then
      FAttrDefIndex[AttDef.Index] := FAttrTag;
  end
  else
    AttDef := nil;
===

ElDef is not assigned, so AttDef is nil...
AttDef holds TAttrDataType which should be recognised as "dtId"...

I hope I got it right so far - but still don't know what the actual problem is...

-----------------------------------------------------------------------------------------------------

That't what I'm currently testing with:

=== test.xml ===

<?xml version="1.0" encoding="UTF-8"?>
<html>
<div id="test">Test</div>
</html>

=== test.pas ===

program test;
{$mode objfpc}{$H+}
Uses
  Classes, XMLRead, DOM;
Var
  Doc : TXMLDocument;
  Parser : TDOMParser;
  Source : TXMLInputSource;
  List   : TStringList;
begin
  List := TStringList.Create;
  List.LoadFromFile('test.xml');
  Parser := TDOMParser.Create;
  try
    Parser.Options.IgnoreComments := true;
    //Parser.Options.Validate := true;
    Source := TXMLInputSource.Create(List.Text);
    try
      Parser.Parse(Source, Doc);
    finally
      Source.Free;
    end;
  finally
    Parser.Free;
  end;
  WriteLn(assigned(Doc.IDs));  // output: false
  List.Free;
end.

======

Daniel Gaspary
to Unknown author

Using Xpath I believe would be something like this:

function ElementById(id: string; doc: TXMLDocument): TDOMElement;
var
   v: TXPathVariable;
begin
     result:=nil;
     try
        v:=EvaluateXPathExpression('' + QuotedStr(id)+']',
             doc.DocumentElement);
        result:=TDOMElement(v.AsNodeSet[0]);
     finally
            FreeAndNil(v);
     end;
end;
Show quoted text 16 lines
A little side note to my statement: > and I don't have a DTD In this case the behaviour of the parser is even right... W3C: Note: Attributes with the name "ID" or "id" are not of type ID unless so defined ---------------------- http://stackoverflow.com/questions/3423430/java-xml-dom-how-are-id-attributes-special But that's not the solution to my problem... :( d.l.i.w _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Daniel Gaspary
to Daniel Gaspary
Daniel Gaspary wrote
Using Xpath I believe would be something like this:

I was missing an If:

function ElementById(id: string; doc: TXMLDocument): TDOMElement;
var
   v: TXPathVariable;
begin
     result:=nil;
     try
        v:=EvaluateXPathExpression('' + QuotedStr(id)+']',
             doc.DocumentElement);
        with v.AsNodeSet do
        begin
             if Count>0
             then
                 result:=TDOMElement(Items[0]);
        end;
     finally
            FreeAndNil(v);
     end;
end;
Unknown author
to Daniel Gaspary
Show quoted text 9 lines
> Using Xpath I believe would be something like this: I was missing an If: function ElementById(id: string; doc: TXMLDocument): TDOMElement; var v: TXPathVariable; begin [..] end;

This does indeed work - thank you :)
It's an acceptable solution for me, as I will need XPath anyway.

Still I think that TDOMDocument.getElementById is better performance-wise - and I don't see a reason why it shouldn't work if TDOMDocument is created by the XMLParser - even if no definition is given...

Is this worth a feature request / bug on mantis?
d.l.i.w

Michael Van Canneyt
to Unknown author
Show quoted text 18 lines
>> Using Xpath I believe would be something like this: > I was missing an If: > > function ElementById(id: string; doc: TXMLDocument): TDOMElement; > var > v: TXPathVariable; > begin > [..] > end; This does indeed work - thank you :) It's an acceptable solution for me, as I will need XPath anyway. Still I think that TDOMDocument.getElementById is better performance-wise - and I don't see a reason why it shouldn't work if TDOMDocument is created by the XMLParser - even if no definition is given... Is this worth a feature request / bug on mantis?

Yes.

Michael.

Benito van der Zander
to Unknown author: working with fcl-xml some questions came together. Every hint is appreciated. 1. No…

Hi,

What is the state of XPath? The readme says 'Should be fairly completed' - can I assume it works for simple tasks?

If you need xpath, you might also want to look at my Internet Tools ( (http://benibela.de/sources_en.html#internettools), they contain a basically complete XPath 2 implementation

Benito

Show quoted text 14 lines
Hi, working with fcl-xml some questions came together. Every hint is appreciated. 1. No matter what I do and which (short & valid) xml file I use - getElementById is always nil (tested with trunk and latest stable 2.6.2) and the same applies to TXMLDocument.IDs. I tried with ReadXMLFile and TDOMParser with different options - but the result is always the same. ... Many thanks for any hint, d.l.i.w