-
Notifications
You must be signed in to change notification settings - Fork 4
/
SaxXForm.cs
143 lines (127 loc) · 5.44 KB
/
SaxXForm.cs
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
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Saxon.Api;
using System.Collections.Generic;
namespace ERPHelper
{
public class SaxonXForm
{
public static string TransformXml(string xmlData, string xslData)
{
string results = "";
string xml = "";
try
{
// Saxon Transformation Start
Processor xsltProcessor = new Processor();
DocumentBuilder documentBuilder = xsltProcessor.NewDocumentBuilder();
documentBuilder.BaseUri = new Uri("file://");
XdmNode xdmNode = documentBuilder.Build(new StringReader(xmlData));
XsltCompiler xsltCompiler = xsltProcessor.NewXsltCompiler();
XsltExecutable xsltExecutable = xsltCompiler.Compile(new StringReader(xslData));
XsltTransformer xsltTransformer = xsltExecutable.Load();
xsltTransformer.InitialContextNode = xdmNode;
Serializer serializer = xsltProcessor.NewSerializer();
using (Stream stream = new MemoryStream())
{
serializer.SetOutputStream(stream);
xsltTransformer.Run(serializer);
// Saxon Transformation End
// Stream to String
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream))
{
xml = reader.ReadToEnd();
}
try
{
XDocument doc = XDocument.Parse(xml);
xml = doc.ToString();
}
catch (Exception) { }
}
// Handle XML Declaration
string version = "1.0";
string encoding = "UTF-8";
string standalone = null;
string declaration = new XDeclaration(version, encoding, standalone).ToString() + Environment.NewLine;
try
{
XDocument xslt = XDocument.Parse(xslData);
XNamespace xsl = "http://www.w3.org/1999/XSL/Transform";
XElement elem = xslt.Root.Element(xsl + "output");
if (elem != null)
{
if (elem.Attribute("version") != null)
{
version = elem.Attribute("version").Value;
}
if (elem.Attribute("encoding") != null)
{
encoding = elem.Attribute("encoding").Value;
}
if (elem.Attribute("standalone") != null)
{
standalone = elem.Attribute("standalone").Value;
}
declaration = new XDeclaration(version, encoding, standalone).ToString() + Environment.NewLine;
// Clear Declaration
if (elem.Attribute("omit-xml-declaration") != null && elem.Attribute("omit-xml-declaration").Value == "yes")
{
declaration = "";
}
if (elem.Attribute("method") != null && elem.Attribute("method").Value != "xml")
{
declaration = "";
}
}
// Attempt to prevent duplicate declaration
if (xml.StartsWith("<?"))
{
declaration = "";
}
}
catch
{
// ignore exception
}
results = declaration + xml;
}
catch (Exception e)
{
throw e;
}
return results;
}
public static Dictionary<int, string> XPath(string xmlData, string xPath)
{
Processor processor = new Processor();
XPathCompiler compiler = processor.NewXPathCompiler();
XDocument xDoc = XDocument.Parse(xmlData);
// Detect namespaces in the document so the user doesn't have to specify them.
var xmlNameSpaceList = ((IEnumerable)xDoc.XPathEvaluate(@"//namespace::*[not(. = ../../namespace::*)]")).Cast<XAttribute>();
foreach (var nsNode in xmlNameSpaceList)
{
compiler.DeclareNamespace(nsNode.Name.LocalName, nsNode.Value);
}
DocumentBuilder documentBuilder = processor.NewDocumentBuilder();
documentBuilder.IsLineNumbering = true;
documentBuilder.BaseUri = new Uri("file://");
XdmNode document = documentBuilder.Build(new StringReader(xmlData));
XPathSelector selector = compiler.Compile(xPath).Load();
selector.ContextItem = document;
XdmValue values = selector.Evaluate();
Dictionary<int, string> lines = new Dictionary<int, string>();
foreach(XdmNode value in values)
{
lines.Add(value.LineNumber - 1, value.ToString());
}
return lines;
}
}
}