1
0
mirror of synced 2024-11-21 13:43:49 +00:00

Support for namespaces in child nodes

This commit is contained in:
Mark van Renswoude 2013-06-21 10:25:49 +00:00
parent be56cf001a
commit cc62e18e4e
4 changed files with 72 additions and 28 deletions

View File

@ -11,6 +11,8 @@ interface
uses uses
Classes, Classes,
SysUtils, SysUtils,
XMLDoc,
xmldom,
XMLIntf; XMLIntf;
@ -28,6 +30,22 @@ type
end; end;
TX2XMLNode = class(TXMLNode)
private
function GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode;
protected
property ChildNodesNS[const ANodeName, ANamespaceURI: DOMString]: IXMLNode read GetChildNodesNS;
end;
TX2XMLNodeCollection = class(TXMLNodeCollection)
private
function GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode;
protected
property ChildNodesNS[const ANodeName, ANamespaceURI: DOMString]: IXMLNode read GetChildNodesNS;
end;
const const
AllTimeFragments = [Low(TXMLTimeFragment)..High(TXMLTimeFragment)]; AllTimeFragments = [Low(TXMLTimeFragment)..High(TXMLTimeFragment)];
@ -122,6 +140,26 @@ type
{ TX2XMLNode }
function TX2XMLNode.GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode;
begin
Result := ChildNodes.FindNode(ANodeName, ANamespaceURI);
if (not Assigned(Result)) and (doNodeAutoCreate in OwnerDocument.Options) then
Result := AddChild(ANodeName, ANamespaceURI);
end;
{ TX2XMLNodeCollection }
function TX2XMLNodeCollection.GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode;
begin
Result := ChildNodes.FindNode(ANodeName, ANamespaceURI);
if (not Assigned(Result)) and (doNodeAutoCreate in OwnerDocument.Options) then
Result := AddChild(ANodeName, ANamespaceURI);
end;
function DateTimeToXML(ADate: TDateTime; AFormat: TXMLDateTimeFormat; ATimeFragments: TXMLTimeFragments): string; function DateTimeToXML(ADate: TDateTime; AFormat: TXMLDateTimeFormat; ATimeFragments: TXMLTimeFragments): string;
var var
formatSettings: TFormatSettings; formatSettings: TFormatSettings;

View File

@ -1334,7 +1334,7 @@ begin
sourceCode.Add('begin'); sourceCode.Add('begin');
if AProperty.HasTargetNamespace then if AProperty.HasTargetNamespace then
sourceCode.Add(' Result := (ChildNodes.FindNode(''%<PropertySourceName>:s'', ''%<Namespace>:s'') as IXML%<PropertyItemName>:s);') sourceCode.Add(' Result := (ChildNodesNS[''%<PropertySourceName>:s'', ''%<Namespace>:s''] as IXML%<PropertyItemName>:s);')
else else
sourceCode.Add(' Result := (ChildNodes[''%<PropertySourceName>:s''] as IXML%<PropertyItemName>:s);'); sourceCode.Add(' Result := (ChildNodes[''%<PropertySourceName>:s''] as IXML%<PropertyItemName>:s);');

View File

@ -151,7 +151,7 @@ const
{ dntElementNS } { dntElementNS }
'function TXML%<Name>:s.Get%<PropertyName>:sIsNil: Boolean;' + CrLf + 'function TXML%<Name>:s.Get%<PropertyName>:sIsNil: Boolean;' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' Result := GetNodeIsNil(ChildNodes.FindNode(''%<PropertySourceName>:s'', ''%<Namespace>:s''));' + CrLf + ' Result := GetNodeIsNil(ChildNodesNS[''%<PropertySourceName>:s'', ''%<Namespace>:s'']);' + CrLf +
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf '' + CrLf
); );
@ -168,7 +168,7 @@ const
{ dntElementNS } { dntElementNS }
'procedure TXML%<Name>:s.Set%<PropertyName>:sIsNil(const Value: Boolean);' + CrLf + 'procedure TXML%<Name>:s.Set%<PropertyName>:sIsNil(const Value: Boolean);' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' SetNodeIsNil(ChildNodes.FindNode(''%<PropertySourceName>:s'', ''%<Namespace>:s''), Value);' + CrLf + ' SetNodeIsNil(ChildNodesNS[''%<PropertySourceName>:s'', ''%<Namespace>:s''], Value);' + CrLf +
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf '' + CrLf
); );
@ -185,14 +185,14 @@ const
{ dntElement } { dntElement }
'function TXML%<Name>:s.Get%<PropertyName>:sText: WideString;' + CrLf + 'function TXML%<Name>:s.Get%<PropertyName>:sText: WideString;' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' Result := ChildNodes.FindNode(''%<PropertySourceName>:s'', ''%<Namespace>:s'').Text;' + CrLf + ' Result := ChildNodesNS[''%<PropertySourceName>:s'', ''%<Namespace>:s''].Text;' + CrLf +
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf '' + CrLf
); );
PropertyImplMethodGetTextAttr = 'function TXML%<Name>:s.Get%<PropertyName>:sText: WideString;' + CrLf + PropertyImplMethodGetTextAttr = 'function TXML%<Name>:s.Get%<PropertyName>:sText: WideString;' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' Result := AttributeNodes[''%<PropertySourceName>:s''].Text;' + CrLf + ' Result := AttributeNodes[''%<PropertySourceName>:s''].Text;' + CrLf +
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf; '' + CrLf;
@ -208,7 +208,7 @@ const
{ dntElementNS } { dntElementNS }
'procedure TXML%<Name>:s.Set%<PropertyName>:sText(const Value: WideString);' + CrLf + 'procedure TXML%<Name>:s.Set%<PropertyName>:sText(const Value: WideString);' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' ChildNodes.FindNode(''%<PropertySourceName>:s'', ''%<Namespace>:s'').NodeValue := Value;' + CrLf + ' ChildNodesNS[''%<PropertySourceName>:s'', ''%<Namespace>:s''].NodeValue := Value;' + CrLf +
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf '' + CrLf
); );
@ -231,7 +231,7 @@ const
{ dntElementNS } { dntElementNS }
'procedure TXML%<Name>:s.Save%<PropertyName>:sToStream(AStream: TStream);' + CrLf + 'procedure TXML%<Name>:s.Save%<PropertyName>:sToStream(AStream: TStream);' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' Base64DecodeToStream(Trim(ChildNodes.FindNode(''%<PropertySourceName>:s'', ''%<Namespace>:s'').Text), AStream);' + CrLf + ' Base64DecodeToStream(Trim(ChildNodesNS[''%<PropertySourceName>:s'', ''%<Namespace>:s''].Text), AStream);' + CrLf +
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf '' + CrLf
); );
@ -248,7 +248,7 @@ const
{ dntElementNS } { dntElementNS }
'procedure TXML%<Name>:s.Save%<PropertyName>:sToFile(const AFileName: string);' + CrLf + 'procedure TXML%<Name>:s.Save%<PropertyName>:sToFile(const AFileName: string);' + CrLf +
'begin' + CrLf + 'begin' + CrLf +
' Base64DecodeToFile(Trim(ChildNodes.FindNode(''%<PropertySourceName>:s'', ''%<Namespace>:s'').Text), AFileName);' + CrLf + ' Base64DecodeToFile(Trim(ChildNodesNS[''%<PropertySourceName>:s'', ''%<Namespace>:s''].Text), AFileName);' + CrLf +
'end;' + CrLf + 'end;' + CrLf +
'' + CrLf '' + CrLf
); );
@ -275,10 +275,10 @@ const
CollectionInterface = 'IXMLNodeCollection'; CollectionInterface = 'IXMLNodeCollection';
CollectionClass = 'TXMLNodeCollection'; CollectionClass = 'TX2XMLNodeCollection';
ItemInterface = 'IXMLNode'; ItemInterface = 'IXMLNode';
ItemClass = 'TXMLNode'; ItemClass = 'TX2XMLNode';
@ -369,7 +369,7 @@ const
{ daGet } { daGet }
( (
{ dntElement } ' %<Destination>:s := ChildNodes[''%<Source>:s''].NodeValue;', { dntElement } ' %<Destination>:s := ChildNodes[''%<Source>:s''].NodeValue;',
{ dntElementNS } ' %<Destination>:s := ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'').NodeValue;', { dntElementNS } ' %<Destination>:s := ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''].NodeValue;',
{ dntAttribute } ' %<Destination>:s := AttributeNodes[''%<Source>:s''].NodeValue;', { dntAttribute } ' %<Destination>:s := AttributeNodes[''%<Source>:s''].NodeValue;',
{ dntNodeValue } ' %<Destination>:s := GetNodeValue;', { dntNodeValue } ' %<Destination>:s := GetNodeValue;',
{ dntCustom } ' %<Destination>:s := %<Source>:s;' { dntCustom } ' %<Destination>:s := %<Source>:s;'
@ -377,7 +377,7 @@ const
{ daSet } { daSet }
( (
{ dntElement } ' ChildNodes[''%<Destination>:s''].NodeValue := %<Source>:s;', { dntElement } ' ChildNodes[''%<Destination>:s''].NodeValue := %<Source>:s;',
{ dntElementNS } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'').NodeValue := %<Source>:s;', { dntElementNS } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''].NodeValue := %<Source>:s;',
{ dntAttribute } ' SetAttribute(''%<Destination>:s'', %<Source>:s);', { dntAttribute } ' SetAttribute(''%<Destination>:s'', %<Source>:s);',
{ dntNodeValue } ' SetNodeValue(%<Source>:s);', { dntNodeValue } ' SetNodeValue(%<Source>:s);',
{ dntCustom } ' %<Destination>:s := %<Source>:s;' { dntCustom } ' %<Destination>:s := %<Source>:s;'
@ -405,13 +405,13 @@ const
( (
{ tcNone } '', { tcNone } '',
{ tcBoolean } '', { tcBoolean } '',
{ tcFloat } ' %<Destination>:s := XMLToFloat(ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'').NodeValue);', { tcFloat } ' %<Destination>:s := XMLToFloat(ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''].NodeValue);',
{ tcDateTime } ' %<Destination>:s := XMLToDateTime(ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'').NodeValue, xdtDateTime);', { tcDateTime } ' %<Destination>:s := XMLToDateTime(ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''].NodeValue, xdtDateTime);',
{ tcDate } ' %<Destination>:s := XMLToDateTime(ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'').NodeValue, xdtDate);', { tcDate } ' %<Destination>:s := XMLToDateTime(ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''].NodeValue, xdtDate);',
{ tcTime } ' %<Destination>:s := XMLToDateTime(ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'').NodeValue, xdtTime);', { tcTime } ' %<Destination>:s := XMLToDateTime(ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''].NodeValue, xdtTime);',
{ tcString } ' %<Destination>:s := ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'').Text;', { tcString } ' %<Destination>:s := ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''].Text;',
{ tcBase64 } ' %<Destination>:s := Base64Decode(Trim(ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'').Text));', { tcBase64 } ' %<Destination>:s := Base64Decode(Trim(ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''].Text));',
{ tcNode } ' %<Destination>:s := ChildNodes.FindNode(''%<Source>:s'', ''%<Namespace>:s'');' { tcNode } ' %<Destination>:s := ChildNodesNS[''%<Source>:s'', ''%<Namespace>:s''];'
), ),
{ dntAttribute } { dntAttribute }
( (
@ -467,14 +467,14 @@ const
{ dntElementNS } { dntElementNS }
( (
{ tcNone } '', { tcNone } '',
{ tcBoolean } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'').NodeValue := BoolToXML(%<Source>:s);', { tcBoolean } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''].NodeValue := BoolToXML(%<Source>:s);',
{ tcFloat } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'').NodeValue := FloatToXML(%<Source>:s);', { tcFloat } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''].NodeValue := FloatToXML(%<Source>:s);',
{ tcDateTime } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'').NodeValue := DateTimeToXML(%<Source>:s, xdtDateTime);', { tcDateTime } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''].NodeValue := DateTimeToXML(%<Source>:s, xdtDateTime);',
{ tcDate } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'').NodeValue := DateTimeToXML(%<Source>:s, xdtDate);', { tcDate } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''].NodeValue := DateTimeToXML(%<Source>:s, xdtDate);',
{ tcTime } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'').NodeValue := DateTimeToXML(%<Source>:s, xdtTime);', { tcTime } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''].NodeValue := DateTimeToXML(%<Source>:s, xdtTime);',
{ tcString } '', { tcString } '',
{ tcBase64 } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'').NodeValue := Base64Encode(%<Source>:s);', { tcBase64 } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''].NodeValue := Base64Encode(%<Source>:s);',
{ tcNode } ' ChildNodes.FindNode(''%<Destination>:s'', ''%<Namespace>:s'') := %<Source>:s;' { tcNode } ' ChildNodesNS[''%<Destination>:s'', ''%<Namespace>:s''] := %<Source>:s;'
), ),
{ dntAttribute } { dntAttribute }
( (

View File

@ -976,6 +976,7 @@ var
propertyType: TXMLDataBindingItem; propertyType: TXMLDataBindingItem;
propertyItem: TXMLDataBindingProperty; propertyItem: TXMLDataBindingProperty;
namespace: string; namespace: string;
schemaDef: IXMLSchemaDef;
begin begin
propertyType := ProcessElement(ASchema, AElement); propertyType := ProcessElement(ASchema, AElement);
@ -991,9 +992,14 @@ begin
AElement.Name, AElement.Name,
AElement.DataType); AElement.DataType);
if not VarIsNull(AElement.SchemaDef.TargetNamespace) then if Assigned(AElement.Ref) then
schemaDef := AElement.Ref.SchemaDef
else
schemaDef := AElement.SchemaDef;
if Assigned(schemaDef) and (not VarIsNull(schemaDef.TargetNamespace)) then
begin begin
namespace := AElement.SchemaDef.TargetNamespace; namespace := schemaDef.TargetNamespace;
if namespace <> Schemas[0].TargetNamespace then if namespace <> Schemas[0].TargetNamespace then
propertyItem.TargetNamespace := namespace; propertyItem.TargetNamespace := namespace;
end; end;