Reputation: 5352
I am trying to transform the following Xsl stylesheet.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/TestPage" xml:space="preserve" >
<ul>
<li class="first complete-tab">
<i class="fa fa-check-circle"></i>Step 1
</li>
<li class="active">
<i class="fa fa-pencil"></i>Step 2
</li>
<li class="last">Step 3</li>
</ul>
</xsl:template>
</xsl:stylesheet>
The code I'm using is as follows.
class Program
{
static void Main(string[] args)
{
string xslFile = "C:\\Projects\\Test.xsl";
XmlDocument doc = new XmlDocument();
XmlElement rootElement = doc.CreateElement("TestPage");
doc.AppendChild(rootElement);
string result = TransformDoc(doc, xslFile);
}
private static string TransformDoc(XmlDocument doc, string transformPath)
{
return TransformDoc(doc.CreateNavigator(), transformPath);
}
static string TransformDoc(XPathNavigator nav, string transformPath, XsltArgumentList args = null)
{
StringBuilder buffer = new StringBuilder();
string result = "A transformation Error Has Occurred";
using (StringWriter stringWriter = new StringWriter(buffer))
using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter))
{
XslCompiledTransform oTransform = new XslCompiledTransform();
XsltSettings oSettings = new XsltSettings(false, true);
oTransform.Load(transformPath, oSettings, null);
if (args == null)
oTransform.Transform(nav, xmlWriter);
else
oTransform.Transform(nav, args, xmlWriter);
result = buffer.ToString();
}
return result;
}
}
The code above transforms the Xsl file, however I'm finding that I'm not getting the desired output.
The result is
<ul>
<li class="first complete-tab">
<i class="fa fa-check-circle" />Step 1
</li>
<li class="active">
<i class="fa fa-pencil" />Step 2
</li>
<li class="last">Step 3</li>
</ul>
For <i class="fa fa-check-circle"></i>Step 1
, I am getting the following <i class="fa fa-check-circle" />
which is breaking my display because of the closing the tag.
Upvotes: 0
Views: 44
Reputation: 70598
According to https://learn.microsoft.com/en-us/dotnet/standard/data/xml/output-options-on-the-xslcompiledtransform-class it says...
If your style sheet uses the xsl:output element and the output type is an XmlWriter object, you should use the XslCompiledTransform.OutputSettings property when you create the XmlWriter object. The XslCompiledTransform.OutputSettings property returns an XmlWriterSettings object that contains information derived from the xsl:output element of a compiled style sheet. This XmlWriterSettings object can be passed to the XmlWriter.Create method to create an XmlWriter object with the correct settings.
So, you need to use an XmlWriter
here and pass the OutputSettings for the compiled transform to it when you create it....
static string TransformDoc(XPathNavigator nav, string transformPath, XsltArgumentList args = null)
{
StringBuilder buffer = new StringBuilder();
string result = "A transformation Error Has Occurred";
XslCompiledTransform oTransform = new XslCompiledTransform();
XsltSettings oSettings = new XsltSettings(false, true);
oTransform.Load(transformPath, oSettings, null);
using (StringWriter stringWriter = new StringWriter(buffer))
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, oTransform.OutputSettings))
{
if (args == null)
oTransform.Transform(nav, xmlWriter);
else
oTransform.Transform(nav, args, xmlWriter);
result = buffer.ToString();
}
return result;
}
Upvotes: 1