I have XML stanza:
<Parameter Name="report_code" Value=""/>
with (automatically generated) Delphi XML binding:
TXMLParameterType = class(TXMLNode, IXMLParameterType)
protected
{ IXMLParameterType }
function Get_Name: WideString;
function Get_Value: WideString;
procedure Set_Name(Value: WideString);
procedure Set_Value(Value: WideString);
end;
function TXMLParameterType.Get_Name: WideString;
begin
Result := AttributeNodes['Name'].Text;
end;
procedure TXMLParameterType.Set_Name(Value: WideString);
begin
SetAttribute('Name', Value);
end;
function TXMLParameterType.Get_Value: WideString;
begin
Result := AttributeNodes['Value'].NodeValue;
end;
procedure TXMLParameterType.Set_Value(Value: WideString);
begin
SetAttribute('Value', Value);
end;
And I see in XMLDoc.pas
that the NodeValue
getter does this:
function TXMLNode.GetNodeValue: OleVariant;
begin
Result := GetText;
if Result = '' then
Result := Null;
end;
That results in an error for my business code:
if Param.HasAttribute('Value') then
Result := Param.Value;
This is beyond comprehension - if there is a ""
-valued attribute Value
, then one should really be able to read this ""
value.
Can't this behavior be configured in Delphi (judging from the Delphi XML code - no)? Is it standard XML behavior?
I checked and such Delphi XMLDoc
code was present both in the older (Delphi 2009) and more recent (Delphi XE 11) versions.
I have XML stanza:
<Parameter Name="report_code" Value=""/>
with (automatically generated) Delphi XML binding:
TXMLParameterType = class(TXMLNode, IXMLParameterType)
protected
{ IXMLParameterType }
function Get_Name: WideString;
function Get_Value: WideString;
procedure Set_Name(Value: WideString);
procedure Set_Value(Value: WideString);
end;
function TXMLParameterType.Get_Name: WideString;
begin
Result := AttributeNodes['Name'].Text;
end;
procedure TXMLParameterType.Set_Name(Value: WideString);
begin
SetAttribute('Name', Value);
end;
function TXMLParameterType.Get_Value: WideString;
begin
Result := AttributeNodes['Value'].NodeValue;
end;
procedure TXMLParameterType.Set_Value(Value: WideString);
begin
SetAttribute('Value', Value);
end;
And I see in XMLDoc.pas
that the NodeValue
getter does this:
function TXMLNode.GetNodeValue: OleVariant;
begin
Result := GetText;
if Result = '' then
Result := Null;
end;
That results in an error for my business code:
if Param.HasAttribute('Value') then
Result := Param.Value;
This is beyond comprehension - if there is a ""
-valued attribute Value
, then one should really be able to read this ""
value.
Can't this behavior be configured in Delphi (judging from the Delphi XML code - no)? Is it standard XML behavior?
I checked and such Delphi XMLDoc
code was present both in the older (Delphi 2009) and more recent (Delphi XE 11) versions.
1 Answer
Reset to default 3As you noted, the NodeValue
property getter will return a Null
variant if the value is empty. You can use VarToWideStr()
to account for that, which will return an empty string if the variant is Null
, eg:
uses
..., System.Variants;
function TXMLParameterType.Get_Value: WideString;
begin
Result := VarToWideStr(AttributeNodes['Value'].NodeValue);
end;
Element.getAttribute()
doesn't distinguish between an empty attribute and an absent attribute - one of the many poor design choices in the DOM API. – Michael Kay Commented Mar 26 at 10:08function GetAttribute( var: String ): Boolean
to check the function's result for attribute existence. One isn't bound toString
's abilities alone. – AmigoJack Commented Mar 26 at 18:58VarToStr()
, which returns an empty string for a null variant. – Remy Lebeau Commented Mar 26 at 19:15