-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
intf.ZUGFeRDXmlHelper.pas
160 lines (141 loc) · 5.5 KB
/
intf.ZUGFeRDXmlHelper.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
{* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.}
unit intf.ZUGFeRDXmlHelper;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes
,System.IOUtils,System.Win.COMObj,System.UITypes,System.StrUtils
,Xml.xmldom,Xml.XMLDoc,Xml.XMLIntf,Xml.XMLSchema
,Xml.Win.msxmldom, Winapi.MSXMLIntf, Winapi.msxml
;
type
TZUGFeRDXmlHelper = class(TObject)
public type
TXMLLoadCallback = procedure(Node : IXMLNode) of object;
public
class procedure LoadFromChilds(const _NodeName : String; _Node : IXMLNode; _Callback : TXMLLoadCallback);
class function FindChild(_Node : IXMLNode; const _NodeName : String; out _Result : IXMLNode) : Boolean;
class function SelectNode(_XnRoot: IXMLDOMNode; const _NodePath: String; out _Result : IXMLDOMNode): Boolean;
class function SelectNodes(_XnRoot: IXMLDOMNode; const _NodePath: String; out _Result : IXMLDOMNodeList): Boolean;
class function SelectNodeText(_XnRoot: IXMLDOMNode; const _NodePath: String): String;
class function FindNode(_XnRoot: IXMLDOMNode; const _NodePath: String): Boolean;
class function PrepareDocumentForXPathQuerys(_Xml : IXMLDocument) : IXMLDOMDocument2;
end;
implementation
{ TXRechnungXMLHelper }
class function TZUGFeRDXmlHelper.SelectNodes(_XnRoot: IXMLDOMNode;
const _NodePath: String; out _Result : IXMLDOMNodeList): Boolean;
begin
Result := false;
_Result := nil;
if not Assigned(_XnRoot) then
exit;
_Result := _XnRoot.selectNodes(_NodePath);
Result := _Result <> nil;
end;
class function TZUGFeRDXmlHelper.SelectNodeText(_XnRoot: IXMLDOMNode; const _NodePath: String): String;
var
node : IXMLDOMNode;
begin
Result := '';
if TZUGFeRDXmlHelper.SelectNode(_XnRoot,_NodePath,node) then
Result := node.Text;
end;
class function TZUGFeRDXmlHelper.SelectNode(_XnRoot: IXMLDOMNode;
const _NodePath: String; out _Result : IXMLDOMNode): Boolean;
begin
Result := false;
_Result := nil;
if not Assigned(_XnRoot) then
exit;
_Result := _XnRoot.selectSingleNode(_NodePath);
Result := _Result <> nil;
end;
class function TZUGFeRDXmlHelper.FindChild(_Node: IXMLNode; const _NodeName: String;
out _Result: IXMLNode): Boolean;
begin
Result := false;
if _Node = nil then
exit;
_Result := _Node.ChildNodes.FindNode(_NodeName,'');
Result := _Result <> nil;
end;
class procedure TZUGFeRDXmlHelper.LoadFromChilds(const _NodeName: String; _Node: IXMLNode;
_Callback: TXMLLoadCallback);
var
Node : IXMLNode;
begin
Node := _Node.ChildNodes.FindNode(_NodeName,'');
if Node = nil then
exit;
_Callback(Node);
end;
class function TZUGFeRDXmlHelper.PrepareDocumentForXPathQuerys(_Xml: IXMLDocument): IXMLDOMDocument2;
var
// hList: IDOMNodeList;
i: Integer;
s, sNSN, sNSUri: string;
sNsLine: string;
begin
Result := nil;
if not _Xml.Active then
exit;
if SameText(_XML.DocumentElement.FindNamespaceURI('udt'), '') then
_XML.DocumentElement.DeclareNamespace('udt', 'urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100');
if SameText(_XML.DocumentElement.FindNamespaceURI('qdt'), '') then
_XML.documentElement.DeclareNamespace('qdt', 'urn:un:unece:uncefact:data:standard:QualifiedDataType:100');
for i := 0 to _XML.DocumentElement.AttributeNodes.Count - 1 do
begin
sNSN := StringReplace(_XML.DocumentElement.AttributeNodes[I].NodeName, 'xmlns:', '', []);
if sNSN = 'xml' then
begin // wenn es als xmlns:xml hinzugefuegt wird bekommt man die meldung das der Namespacename xml nicht verwendet werden darf...
sNSN := 'xmlns:MyXml';
sNSUri := _XML.DocumentElement.AttributeNodes[I].nodeValue;
end
else
if sNSN = 'xmlns' then
begin // den Default Namespace mit einem Namen versehen, damit XPath drauf zugreifen kann.
sNSN := 'xmlns:dn';
sNSUri := _XML.DocumentElement.AttributeNodes[I].nodeValue;
end
else
begin // alle anderen Namespace auch fuer XPath bekannt machen
sNSN := _XML.DocumentElement.AttributeNodes[I].nodeName;
sNSUri := _XML.DocumentElement.AttributeNodes[I].nodeValue;
end;
s := sNSN + '="'+sNSUri+'"';
if Pos(AnsiUpperCase(sNsLine), AnsiUpperCase(s)) > 0 then
continue;
if Pos(AnsiUpperCase(sNsLine), AnsiUpperCase(sNSN+'="')) > 0 then
continue;
if SameText(sNSN,'xsi:schemaLocation') then
continue;
sNsLine := ' '+s + sNsLine;
end;
sNsLine := trim(sNsLine);
Result := CoDOMDocument60.Create;
Result.loadXML(_Xml.XML.Text);
Result.setProperty('SelectionLanguage', 'XPath'); // ab 4.0 ist SelectionLanguage eh immer XPath
Result.setProperty('SelectionNamespaces', sNsLine) ;
end;
class function TZUGFeRDXmlHelper.FindNode(_XnRoot: IXMLDOMNode; const _NodePath: String): Boolean;
var
Node : IXMLDOMNode;
begin
Result := TZUGFeRDXmlHelper.SelectNode(_XnRoot,_NodePath,Node);
end;
end.