Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SelectXml.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
3using System.Xml;
8
10{
15 {
25 : base(Xml, Name, Start, Length, Expression)
26 {
27 }
28
32 public override string FunctionName => nameof(SelectXml);
33
37 public override string[] DefaultArgumentNames => new string[] { "XML", "XPath" };
38
47 {
48 object Obj = Argument1.AssociatedObjectValue;
49 XmlNodeList Result;
50
51 if (Obj is null)
52 return ObjectValue.Null;
53 else if (Obj is XmlNode N)
54 Result = Evaluate(N, Argument2.AssociatedObjectValue?.ToString() ?? string.Empty);
55 else
56 throw new ScriptRuntimeException("XPath expression only operate on XML.", this);
57
58 int i, c = Result.Count;
59
60 switch (c)
61 {
62 case 0:
63 return ObjectValue.Null;
64
65 case 1:
66 return ToElement(Result[0]);
67
68 default:
69 IElement[] Items = new IElement[c];
70
71 for (i = 0; i < c; i++)
72 Items[i] = ToElement(Result[i]);
73
74 return Operators.Vectors.VectorDefinition.Encapsulate(Items, false, this);
75 }
76 }
77
86 {
87 return Task.FromResult(this.EvaluateScalar(Argument1, Argument2, Variables));
88 }
89
90 internal static XmlNodeList Evaluate(XmlNode N, string XPath)
91 {
92 XmlNamespaceManager NamespaceManager;
93 XmlNodeList Result;
94 XmlElement Root;
95
96 if (N is XmlDocument Doc)
97 {
98 NamespaceManager = new XmlNamespaceManager(Doc.NameTable);
99 Root = Doc.DocumentElement;
100 }
101 else if (N is XmlElement E && !(N.OwnerDocument is null))
102 {
103 NamespaceManager = new XmlNamespaceManager(N.OwnerDocument.NameTable);
104 Root = E;
105 }
106 else
107 {
108 NamespaceManager = null;
109 Root = null;
110 }
111
112 if (NamespaceManager is null)
113 Result = N.SelectNodes(XPath);
114 else
115 {
116 if (XPath.Contains("default:"))
117 {
118 string Namespace = null;
119
120 if (string.IsNullOrEmpty(Root.Prefix) && !string.IsNullOrEmpty(Root.NamespaceURI))
121 Namespace = Root.NamespaceURI;
122 else
123 {
124 LinkedList<XmlElement> ToProcess = new LinkedList<XmlElement>();
125
126 foreach (XmlNode N2 in Root.ChildNodes)
127 {
128 if (N2 is XmlElement E)
129 ToProcess.AddLast(E);
130 }
131
132 while (!(ToProcess.First is null))
133 {
134 Root = ToProcess.First.Value;
135 ToProcess.RemoveFirst();
136
137 if (string.IsNullOrEmpty(Root.Prefix) && !string.IsNullOrEmpty(Root.NamespaceURI))
138 {
139 Namespace = Root.NamespaceURI;
140 break;
141 }
142
143 foreach (XmlNode N2 in Root.ChildNodes)
144 {
145 if (N2 is XmlElement E)
146 ToProcess.AddLast(E);
147 }
148 }
149 }
150
151 if (!string.IsNullOrEmpty(Namespace) && !NamespaceManager.HasNamespace(Namespace))
152 NamespaceManager.AddNamespace("default", Namespace);
153 }
154
155 Result = N.SelectNodes(XPath, NamespaceManager);
156 }
157
158 return Result;
159 }
160
166 public static IElement ToElement(XmlNode Node)
167 {
168 if (Node is XmlText Text)
169 return ToElement(Text.Value);
170 else if (Node is XmlCDataSection CData)
171 return ToElement(CData.Value);
172 else if (Node is XmlAttribute Attr)
173 return ToElement(Attr.Value);
174 else
175 {
176 if (Node.HasChildNodes && Node.FirstChild == Node.LastChild && Node.FirstChild is XmlText Text2)
177 return ToElement(Text2.Value);
178 else
179 return new ObjectValue(Node);
180 }
181 }
182
183 private static IElement ToElement(string s)
184 {
185 if (Expression.TryParse(s, out double d))
186 return new DoubleNumber(d);
187 else
188 return new StringValue(s);
189 }
190
191 }
192}
Class managing a script expression.
Definition: Expression.cs:39
static bool TryParse(string s, out double Value)
Tries to parse a double-precision floating-point value.
Definition: Expression.cs:4517
Base class for funcions of two scalar variables.
ScriptNode Argument2
Function argument 2.
ScriptNode Argument1
Function argument 1.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Collection of variables.
Definition: Variables.cs:25
Performs an XML slection using XPATH.
Definition: SelectXml.cs:15
override string FunctionName
Name of the function
Definition: SelectXml.cs:32
static IElement ToElement(XmlNode Node)
Encapsulates an XML Node for use in script.
Definition: SelectXml.cs:166
SelectXml(ScriptNode Xml, ScriptNode Name, int Start, int Length, Expression Expression)
Performs an XML slection using XPATH.
Definition: SelectXml.cs:24
override IElement EvaluateScalar(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: SelectXml.cs:46
override string[] DefaultArgumentNames
Default Argument names
Definition: SelectXml.cs:37
override Task< IElement > EvaluateScalarAsync(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: SelectXml.cs:85
Basic interface for all types of elements.
Definition: IElement.cs:20
IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.