Skip to content

Instantly share code, notes, and snippets.

@adamnew123456
Created March 29, 2017 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamnew123456/de3f0d7a14c0bc841a0bcb68c31b8565 to your computer and use it in GitHub Desktop.
Save adamnew123456/de3f0d7a14c0bc841a0bcb68c31b8565 to your computer and use it in GitHub Desktop.
XSN XSLT
using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
namespace XSLTEvaluator
{
public class MainClass
{
public static void Main(string[] Args)
{
var transform = new XslCompiledTransform();
using (var xsl_stream = new FileStream(Args[0], FileMode.Open))
{
var xsl_reader = XmlReader.Create(xsl_stream);
Console.WriteLine("Loading XSL file: " + Args[0]);
// We need to patch up xdTextBox elements into actual HTML text entries
var xsl_document = new XmlDocument();
xsl_document.Load(xsl_reader);
// We modify this list later, so we can't rely on the enumerator in the loop
var spans = xsl_document.GetElementsByTagName("span");
var possible_textboxes = new XmlNode[spans.Count];
for (var i = 0; i < spans.Count; i++)
{
possible_textboxes[i] = (XmlNode)spans[i];
}
foreach (var possible_textbox in possible_textboxes)
{
var class_attr = possible_textbox.Attributes.GetNamedItem("class");
if (class_attr != null && class_attr.Value == "xdTextBox")
{
Console.WriteLine("... Preparing to replace <span ... class=\"xdTexBox\" ...> with <input>");
var new_textbox = xsl_document.CreateElement("input");
var new_attributes = new XmlAttribute[possible_textbox.Attributes.Count];
possible_textbox.Attributes.CopyTo(new_attributes, 0);
foreach (var new_attribute in new_attributes)
{
new_textbox.Attributes.Append(new_attribute);
}
Console.WriteLine("... Copied " + new_attributes.Length + " attributes");
var new_textbox_type_attr = xsl_document.CreateAttribute("type");
new_textbox_type_attr.Value = "input";
new_textbox.Attributes.Append(new_textbox_type_attr);
new_textbox.InnerXml = possible_textbox.InnerXml;
Console.WriteLine("... Performing replacement");
possible_textbox.ParentNode.InsertAfter(new_textbox, possible_textbox);
possible_textbox.ParentNode.RemoveChild(possible_textbox);
}
}
transform.Load(xsl_document);
}
using (var xml_stream = new FileStream(Args[1], FileMode.Open))
{
Console.WriteLine("Loading XML template: " + Args[1]);
var xml_reader = XmlReader.Create(xml_stream);
Console.WriteLine("Generating out.html");
var xml_writer = XmlWriter.Create("out.html");
transform.Transform(xml_reader, xml_writer);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment