//
// System.Xml.Xsl.XslTransformTests.cs
//
// Author:
// Atsushi Enomoto
//
// (C) 2002 Atsushi Enomoto
//
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using NUnit.Framework;
namespace MonoTests.System.Xml.Xsl
{
[TestFixture]
public class XslTransformTests
{
static readonly string end_of_line = "\r\n";
XmlDocument doc;
XslTransform xslt;
XmlDocument result;
[SetUp]
public void GetReady()
{
doc = new XmlDocument ();
xslt = new XslTransform ();
result = new XmlDocument ();
}
[Test]
public void TestBasicTransform ()
{
string output = Path.Combine (Path.GetTempPath (), "result.xml");
doc.LoadXml ("");
xslt.Load ("Test/XmlFiles/xsl/empty.xsl");
xslt.Transform ("Test/XmlFiles/xsl/empty.xsl", output);
result.Load (output);
Assert.AreEqual (2, result.ChildNodes.Count, "count");
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void InvalidStylesheet ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("");
XslTransform t = new XslTransform ();
t.Load (doc);
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void EmptyStylesheet ()
{
XmlDocument doc = new XmlDocument ();
XslTransform t = new XslTransform ();
t.Load (doc);
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void InvalidStylesheet2 ()
{
string xsl = @"
";
XslTransform xslt = new XslTransform ();
xslt.Load (new XPathDocument (new XmlTextReader (xsl, XmlNodeType.Document, null)));
}
[Test]
[Category ("MobileNotWorking")]
public void MsxslTest() {
string _styleSheet = @"
";
StringReader stringReader = new StringReader(_styleSheet);
XslTransform transform = new XslTransform();
XmlTextReader reader = new XmlTextReader(stringReader);
transform.Load(reader, new XmlUrlResolver(), AppDomain.CurrentDomain.Evidence);
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture);
XsltArgumentList arguments = new XsltArgumentList();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("");
// Do transformation
transform.Transform(xmlDoc, new XsltArgumentList(), writer, new XmlUrlResolver());
Assert.AreEqual ("test".PadRight(20), sb.ToString());
}
[Test]
public void MSXslNodeSet ()
{
string xsl = @"
:
";
StringWriter sw = new StringWriter ();
XslTransform t = new XslTransform ();
t.Load (new XPathDocument (new StringReader (xsl)));
t.Transform (new XPathDocument (new XmlTextReader (new StringReader (""))), null, sw);
Assert.AreEqual (@"foo: Afoo: Bfoo: C", sw.ToString ());
}
[Test]
[ExpectedException (typeof (XPathException))]
public void MSXslNodeSetRejectsNodeSet ()
{
string xsl = @"
:
";
StringWriter sw = new StringWriter ();
XslTransform t = new XslTransform ();
t.Load (new XPathDocument (new StringReader (xsl)));
t.Transform (new XPathDocument (new XmlTextReader (new StringReader (""))), null, sw);
}
[Test]
[Category ("MobileNotWorking")]
public void MSXslFormatDate ()
{
var arguments = new XsltArgumentList();
arguments.AddParam("date", "", new DateTime (2010, 11, 22, 5, 4, 3));
string xsl = @"
The current date is and current time is .
";
StringWriter sw = new StringWriter ();
var t = new XslCompiledTransform ();
t.Load (new XPathDocument (new StringReader (xsl)));
t.Transform (new XPathDocument (new XmlTextReader (new StringReader (""))), arguments, sw);
Assert.AreEqual ("The current date is 22 November 2010 and current time is 05:04.
", sw.ToString ());
}
[Test]
public void EvaluateEmptyVariableAsBoolean ()
{
string xsl = @"
true
";
XslTransform t = new XslTransform ();
t.Load (new XPathDocument (new StringReader (xsl)));
StringWriter sw = new StringWriter ();
t.Transform (
new XPathDocument (new StringReader ("")),
null,
sw);
Assert.IsTrue (sw.ToString ().IndexOf ("true") > 0);
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void NotAllowedPatternAxis ()
{
string xsl = @"
";
new XslTransform ().Load (new XPathDocument (
new StringReader (xsl)));
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void ImportIncorrectlyLocated ()
{
string xsl = @"
";
new XslTransform ().Load (new XPathDocument (
new StringReader (xsl)));
}
private WeakReference StylesheetLoad (XslTransform t, string xsl)
{
XPathDocument doc = new XPathDocument (
new StringReader (xsl));
WeakReference wr = new WeakReference (doc);
t.Load (doc);
return wr;
}
private WeakReference StylesheetTransform (XslTransform t, string xml)
{
XPathDocument doc = new XPathDocument (
new StringReader (xml));
WeakReference wr = new WeakReference (doc);
t.Transform (doc, null, TextWriter.Null, null);
return wr;
}
[Test]
// bug #75663.
public void ErrorOnDocumentResolution ()
{
// XslTransform recovers from errors on document resolution.
string xslText = @"
xx
";
string xmlText = @"";
XslTransform transform = new XslTransform ();
XPathDocument doc = new XPathDocument (
new XmlTextReader ("a.xsl", new StringReader (xslText)));
transform.Load (doc);
XPathDocument xmlDocument = new XPathDocument (new StringReader (xmlText));
StringWriter sw = new StringWriter ();
transform.Transform (xmlDocument, null, sw);
Assert.AreEqual (
"" +
"xx", sw.ToString ());
}
// bug #76046
[Test]
public void LoadStyleFromNonRoot ()
{
XmlDocument doc = new XmlDocument ();
XslTransform xslt = new XslTransform ();
doc.LoadXml ("");
XmlNode node = doc.ChildNodes [0].ChildNodes [1];
xslt.Load (node, null, null);
}
[Test]
public void ReturnEmptyResultsAsXmlReader ()
{
// bug #76115
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("");
XslTransform xslt = new XslTransform ();
xslt.Load (doc, null, null);
XmlReader reader = xslt.Transform(doc, null, new XmlUrlResolver ());
reader.Read ();
// another case - with xsl:output standalone='yes'
doc.LoadXml ("");
xslt = new XslTransform ();
xslt.Load (doc, null, null);
reader = xslt.Transform (doc, null, new XmlUrlResolver ());
while (!reader.EOF)
reader.Read (); // btw no XMLdecl output.
}
[Test] // bug #76530
// http://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element
// "If the namespace attribute is not present then the QName
// is expanded into an expanded-name using the namespace
// declarations in effect for the xsl:element element,
// including any default namespace declaration."
public void LREDefaultNamespace ()
{
string xsl = @"
";
string xml = "";
XslTransform t = new XslTransform ();
t.Load (new XPathDocument (new StringReader (xsl)));
StringWriter sw = new StringWriter ();
XmlTextWriter xw = new XmlTextWriter (sw);
t.Transform (
new XPathDocument (new StringReader (xml)),
null, xw);
Assert.AreEqual ("",
sw.ToString ());
string xsl2 = @"
";
string xml2 = "";
t.Load (new XPathDocument (new StringReader (xsl2)));
sw = new StringWriter ();
xw = new XmlTextWriter (sw);
t.Transform (
new XPathDocument (new StringReader (xml2)),
null, xw);
Assert.AreEqual ("",
sw.ToString ());
}
[Test]
// http://lists.ximian.com/pipermail/mono-devel-list/2005-November/015812.html
public void WhitespaceHandling ()
{
string ref_out = @"XML
Extensible Markup language
Great stuffs
XSLT
Extensible Markup language
Great stuffs
XPATH
Extensible Markup language
Great stuffs
XSD
Extensible Markup language
Great stuffs
";
XmlDocument d = new XmlDocument ();
d.Load ("Test/XmlFiles/xsl/91834.xml");
XslTransform t = new XslTransform ();
t.Load ("Test/XmlFiles/xsl/91834.xsl");
StringWriter sw_raw = new StringWriter ();
t.Transform (d, null, sw_raw);
Assert.AreEqual (ref_out.Replace ("\r\n", "\n"), sw_raw.ToString ().Replace ("\r\n", "\n"));
}
// http://support.microsoft.com/default.aspx?scid=kb;en-us;829014
[Test]
public void EmptyNodeSetSort ()
{
string xmlFragment = @"
Steve
IT
C++
C#
John
IT
VB.NET
SQl Server
";
string xsltFragment = @"
";
XmlTextReader xmlRdr = new XmlTextReader (new StringReader (xmlFragment));
XmlTextReader xsltRdr = new XmlTextReader (new StringReader (xsltFragment));
XslTransform stylesheet = new XslTransform ();
stylesheet.Load (xsltRdr, new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
StringWriter sw = new StringWriter ();
stylesheet.Transform (new XPathDocument (xmlRdr), new XsltArgumentList (),
sw, new XmlUrlResolver ());
Assert.AreEqual (0, sw.ToString ().Length);
}
// http://support.microsoft.com/default.aspx?scid=kb;en-us;834667
[Test]
//[Category ("NotDotNet")]
public void LocalParameter ()
{
string xsltFragment = @"
/
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
StringWriter sw = new StringWriter ();
XsltArgumentList xsltArgs = new XsltArgumentList ();
xsltArgs.AddParam ("param1", string.Empty, "global-param1-arg");
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual ("local-param1-arg/local-param2-arg", sw.ToString ());
}
[Test]
public void Output_Standalone ()
{
StringWriter sw = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format(xsltFragment, "standalone=\"yes\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"", sw.ToString (), "#1");
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "standalone=\"no\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"", sw.ToString (), "#2");
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#3");
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void Output_Standalone_Invalid ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
[Test]
public void Output_Standalone_ForwardsCompatible ()
{
StringWriter sw = new StringWriter ();
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
string xsltFragment = @"
";
// standalone is "Yes"
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "Yes"))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
// standalone is empty
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void Output_Standalone_Empty ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
[Test]
public void Output_OmitXmlDeclaration ()
{
StringWriter sw = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "omit-xml-declaration=\"yes\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual ("", sw.ToString (), "#1");
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "omit-xml-declaration=\"no\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void Output_OmitXmlDeclaration_Invalid ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void Output_OmitXmlDeclaration_Empty ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
[Test]
public void Output_OmitXmlDeclaration_ForwardsCompatible ()
{
StringWriter sw = new StringWriter ();
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
string xsltFragment = @"
";
// omit-xml-declaration is "Yes"
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "Yes"))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
// omit-xml-declaration is empty
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
}
[Test]
public void Output_DocType_Xml ()
{
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
// set both doctype-system and doctype-public
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
StringWriter sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"" +
"", sw.ToString (), "#1");
// only set doctype-public
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
// only set doctype-system
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"" +
"", sw.ToString (), "#3");
// set empty doctype-public and empty doctype-system
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"" +
"", sw.ToString (), "#4");
// set empty doctype-public
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#5");
// set empty doctype-system
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"" +
"", sw.ToString (), "#6");
}
[Test]
public void Output_DocType_Html ()
{
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
// set both doctype-system and doctype-public
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
StringWriter sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
// only set doctype-public
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
// only set doctype-system
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#3");
// set empty doctype-public and empty doctype-system
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#4");
// set empty doctype-public
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#5");
// set empty doctype-system
xsltFragment = @"
";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#6");
}
[Test]
public void Output_Indent_Xml_DocType ()
{
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// set indent to yes
StringWriter sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"yes\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
"", end_of_line), sw.ToString (), "#1");
// set indent to no
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"no\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"" +
"", sw.ToString (),
"#2");
// indent not set
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"" +
"", sw.ToString (),
"#3");
}
[Test]
public void Output_Indent_Html_DocType ()
{
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// set indent to yes
StringWriter sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"yes\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", end_of_line), sw.ToString (), "#1");
// set indent to no
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"no\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"" +
"",
Environment.NewLine), sw.ToString (), "#2");
// indent not set
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"", end_of_line), sw.ToString (), "#3");
}
[Test]
public void Output_Indent_Xml ()
{
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// set indent to yes
StringWriter sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"yes\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" {0}" +
" {0}" +
"", end_of_line), sw.ToString (), "#1");
// set indent to no
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"no\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (),
"#2");
// indent not set
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (),
"#3");
}
[Test]
public void Output_Indent_Html ()
{
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
something
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// set indent to yes
StringWriter sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"yes\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
" {0}" +
" {0}" +
"
{0}" +
" something
{0}" +
" {0}" +
"
{0}" +
"
{0}" +
"
{0}" +
" {0}" +
" {0}" +
" {0}" +
"
{0}" +
" {0}" +
"", end_of_line), sw.ToString (), "#1");
// set indent to no
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "indent=\"no\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"something
" +
"
",
Environment.NewLine), sw.ToString (), "#2");
// indent not set
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
" {0}" +
" {0}" +
"
{0}" +
" something
{0}" +
" {0}" +
"
{0}" +
"
{0}" +
" {0}" +
" {0}" +
" {0}" +
" {0}" +
"
{0}" +
" {0}" +
"", end_of_line), sw.ToString (), "#3");
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void Output_Indent_Invalid ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void Output_Indent_Empty ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
[Test]
public void Output_Indent_ForwardsCompatible ()
{
StringWriter sw = new StringWriter ();
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
string xsltFragment = @"
";
// indent is "Yes"
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "Yes"))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
// indent is empty
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, ""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
}
[Test]
public void Output_MediaType ()
{
StringWriter sw = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString ());
}
[Test]
public void Output_Encoding_TextWriter ()
{
StringWriter sw = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// no encoding
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, string.Empty))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
// valid encoding
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format(xsltFragment, "encoding=\"iso-8859-1\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
// invalid encoding
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "encoding=\"doesnotexist\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
// empty encoding
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "encoding=\"\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#3");
}
[Test]
public void Output_Encoding_Stream ()
{
MemoryStream ms = null;
string result = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// no encoding
ms = new MemoryStream ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, string.Empty))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, ms, new XmlUrlResolver ());
ms.Position = 0;
using (StreamReader sr = new StreamReader (ms, true)) {
result = sr.ReadToEnd ();
}
Assert.AreEqual (
"" +
"", result, "#1");
// valid encoding
ms = new MemoryStream ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "encoding=\"iso-8859-1\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, ms, new XmlUrlResolver ());
ms.Position = 0;
using (StreamReader sr = new StreamReader (ms, true)) {
result = sr.ReadToEnd ();
}
Assert.AreEqual (
"" +
"", result, "#2");
// invalid encoding
ms = new MemoryStream ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "encoding=\"doesnotexist\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, ms, new XmlUrlResolver ());
ms.Position = 0;
using (StreamReader sr = new StreamReader (ms, true)) {
result = sr.ReadToEnd ();
}
Assert.AreEqual (
"" +
"", result, "#3");
// empty encoding
ms = new MemoryStream ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "encoding=\"\""))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, ms, new XmlUrlResolver ());
ms.Position = 0;
using (StreamReader sr = new StreamReader (ms, true)) {
result = sr.ReadToEnd ();
}
Assert.AreEqual (
"" +
"", result, "#4");
}
[Test]
public void Output_Version ()
{
StringWriter sw = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// version 1.0
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "1.0"))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
// version 2.0
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "2.0"))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#2");
// version BLABLA
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, "BLABLA"))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#3");
}
[Test]
public void Output_Method_Html_TextWriter ()
{
string options = null;
StringWriter sw = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
Output Test
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// indent not set, media-type not set
sw = new StringWriter ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, string.Empty))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
"{0}" +
"{1}{0}" +
"{1}{1}{0}" +
"{1}{1}Output Test{0}" +
"{1}{0}" +
"{1}{0}" +
"{1}{1}
{0}" +
"{1}{0}" +
"", end_of_line, " "), sw.ToString (), "#1");
// indent no, media-type not set
options = "indent=\"no\"";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, options))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{1}{0}" +
"{1}{1}{0}" +
"{1}{1}Output Test{0}" +
"{1}{0}" +
"{1}{0}" +
"{1}{1}
{0}" +
"{1}{0}" +
"", string.Empty, string.Empty), sw.ToString (), "#2");
// indent yes, media-type "bla", omit-xml-declaration "no"
options = "indent=\"yes\" media-type=\"bla\"" +
" encoding=\"iso-8859-1\" omit-xml-declaration=\"no\"";
sw.GetStringBuilder ().Length = 0;
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, options))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{1}{0}" +
"{1}{1}{0}" +
"{1}{1}Output Test{0}" +
"{1}{0}" +
"{1}{0}" +
"{1}{1}
{0}" +
"{1}{0}" +
"", end_of_line, " "), sw.ToString (), "#3");
}
[Test]
public void Output_Method_Html_Stream ()
{
string options = null;
MemoryStream ms = null;
string result = null;
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
string xsltFragment = @"
Output Test
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
// indent not set, media-type not set
ms = new MemoryStream ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, string.Empty))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, ms, new XmlUrlResolver ());
ms.Position = 0;
using (StreamReader sr = new StreamReader (ms, true)) {
result = sr.ReadToEnd ();
}
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{1}{0}" +
"{1}{1}{0}" +
"{1}{1}Output Test{0}" +
"{1}{0}" +
"{1}{0}" +
"{1}{1}
{0}" +
"{1}{0}" +
"", end_of_line, " "), result, "#1");
// indent no, media-type not set
options = "indent=\"no\"";
ms = new MemoryStream ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, options))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, ms, new XmlUrlResolver ());
ms.Position = 0;
using (StreamReader sr = new StreamReader (ms, true)) {
result = sr.ReadToEnd ();
}
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{1}{0}" +
"{1}{1}{0}" +
"{1}{1}Output Test{0}" +
"{1}{0}" +
"{1}{0}" +
"{1}{1}
{0}" +
"{1}{0}" +
"", string.Empty, string.Empty), result, "#2");
// indent yes, media-type "bla", omit-xml-declaration "no"
options = "indent=\"yes\" media-type=\"bla\"" +
" encoding=\"iso-8859-1\" omit-xml-declaration=\"no\"";
ms = new MemoryStream ();
xsltProcessor.Load (new XmlTextReader (new StringReader (
string.Format (xsltFragment, options))),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, ms, new XmlUrlResolver ());
ms.Position = 0;
using (StreamReader sr = new StreamReader (ms, true)) {
result = sr.ReadToEnd ();
}
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{1}{0}" +
"{1}{1}{0}" +
"{1}{1}Output Test{0}" +
"{1}{0}" +
"{1}{0}" +
"{1}{1}
{0}" +
"{1}{0}" +
"", end_of_line, " "), result, "#3");
}
[Test]
[ExpectedException (typeof (XsltCompileException))]
public void Output_Unknown_Attribute ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
[Test]
public void Output_Unknown_Attribute_ForwardsCompatible ()
{
StringWriter sw = new StringWriter ();
XsltArgumentList xsltArgs = new XsltArgumentList ();
XslTransform xsltProcessor = new XslTransform ();
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("");
string xsltFragment = @"
";
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, xsltArgs, sw, new XmlUrlResolver ());
Assert.AreEqual (
"" +
"", sw.ToString (), "#1");
}
[Test]
public void Output_Unknown_Attribute_NonDefaultNamespace ()
{
string xsltFragment = @"
";
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
}
// http://support.microsoft.com/kb/832757/en-us
[Test]
public void FormatNumber_KB_832757 ()
{
string xsltFragment = @"
|
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("1000");
StringWriter sw = new StringWriter ();
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, new XsltArgumentList (), sw,
new XmlUrlResolver ());
Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" {0}" +
" 1,000 | {0}" +
"
{0}" +
"
", end_of_line), sw.ToString ());
}
// http://support.microsoft.com/kb/293469/en-us
[Test]
public void FormatNumber_KB_293469 ()
{
string xsltFragment = @"
";
XmlDocument xmlDoc = new XmlDocument ();
xmlDoc.LoadXml ("44442.7");
StringWriter sw = new StringWriter ();
XslTransform xsltProcessor = new XslTransform ();
xsltProcessor.Load (new XmlTextReader (new StringReader (xsltFragment)),
new XmlUrlResolver (), AppDomain.CurrentDomain.Evidence);
xsltProcessor.Transform (xmlDoc, new XsltArgumentList (), sw,
new XmlUrlResolver ());
Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
"{0}" +
"{0}" +
" 44.442,70{0}" +
"", end_of_line), sw.ToString ());
}
[Test]
[Category ("NotDotNet")]
// MS bug: the output must be well-formed XML general entity
// (doctype must not appear after text nodes).
public void DocTypeAfterText ()
{
string xsl = @"
a
";
XslTransform t = new XslTransform ();
t.Load (new XmlTextReader (xsl, XmlNodeType.Document, null));
XmlDocument doc = new XmlDocument ();
try {
t.Transform (new XPathDocument (
new StringReader ("")),
null, TextWriter.Null);
Assert.Fail ("should fail; doctype is not allowed after whitespace. See XSLT 1.0 section 16.1.");
} catch {
}
}
[Test]
public void StripSpace ()
{
string result = @"
MonoDevelop
ProjectTemplates
FileTemplates
IDE
StartupHandlers
FileTemplateTypes
Workspace
Applications
Services
";
XslTransform t = new XslTransform ();
t.Load ("Test/XmlFiles/xsl/stripspace.xsl");
XPathDocument d = new XPathDocument (
"Test/XmlFiles/xsl/stripspace.xml", XmlSpace.Preserve);
StringWriter sw = new StringWriter ();
t.Transform (d, null, sw);
Assert.AreEqual (result, sw.ToString ());
}
[Test]
public void CurrentInSelect ()
{
XslTransform t = new XslTransform ();
t.Load ("Test/XmlFiles/xsl/current-in-select.xsl");
XPathDocument d = new XPathDocument (
"Test/XmlFiles/xsl/current-in-select.xml", XmlSpace.Preserve);
StringWriter sw = new StringWriter ();
t.Transform (d, null, sw);
using (StreamReader sr = File.OpenText (
"Test/XmlFiles/xsl/current-in-select.ref")) {
string s = sr.ReadToEnd ();
Assert.AreEqual (s, sw.ToString ());
}
}
[Test]
public void CallTemplateSignificantWhitespace ()
{
// bug #82357
string xsl = @"
";
XslTransform t = new XslTransform ();
t.Load (new XmlTextReader (new StringReader (xsl)));
t.Transform (new XPathDocument (new StringReader ("")), null, new XmlTextWriter (TextWriter.Null));
}
[Test]
public void Bug82493 ()
{
XslTransform transformationObj = new XslTransform ();
transformationObj.Load ("Test/XmlFiles/xsl/82493.xsl");
XmlDocument doc = new XmlDocument ();
doc.Load ("Test/XmlFiles/xsl/82493.xml");
XslExtensionClass GetCalculation = new XslExtensionClass ("a4");
XsltArgumentList xslExtensionArgList = new XsltArgumentList ();
xslExtensionArgList.AddExtensionObject ("urn:e", GetCalculation);
XmlTextWriter writer = new XmlTextWriter (TextWriter.Null);
transformationObj.Transform (doc, xslExtensionArgList, writer, null);
}
[Test]
public void BugNovell325482 ()
{
XmlDocument doc = new XmlDocument ();
doc.Load ("Test/XmlFiles/xsl/325482.xml");
XslTransform xt = new XslTransform ();
xt.Load ("Test/XmlFiles/xsl/325482.xsl");
xt.Transform (doc, null, TextWriter.Null);
}
public class XslExtensionClass
{
private string paperType = String.Empty;
public XslExtensionClass (string paperType)
{
this.paperType = paperType;
}
public int GetPageDimensions ()
{
switch (this.paperType) {
case "a4":
return 210297;
case "letter":
return 216279;
default:
throw new ArgumentException ("Unknown page size type: " + this.paperType);
}
}
}
[Test] // bug #348786
public void ValueOfContainsExternalElements ()
{
string xsl = @"
";
new XslTransform ().Load (new XmlTextReader (xsl, XmlNodeType.Document, null));
}
[Test] // bug #349035
public void CompareRTFAsStringNotNodeset ()
{
XslTransform xslt = new XslTransform ();
xslt.Load (new XmlTextReader (new StringReader (@"
bar is not empty:''
bar is empty
"
)));
XPathDocument input = new XPathDocument (new StringReader (
@"
"
));
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
xslt.Transform (input, null, xtw);
string expected = "bar is not empty:'HaHa'bar is empty";
Assert.AreEqual (expected, sw.ToString ());
}
[Test]
public void Bug349111 ()
{
XslTransform xslt = new XslTransform ();
xslt.Load (new XmlTextReader (new StringReader (@"
"
)));
XPathDocument input = new XPathDocument (new StringReader (@"
Harry Potter 29.99
"
));
StringWriter sw = new StringWriter ();
xslt.Transform (input, null, new XmlTextWriter (sw));
string expected = "Harry Potter29.99";
Assert.AreEqual (expected, sw.ToString ());
}
[Test]
public void Bug351939 ()
{
XslTransform xslt = new XslTransform ();
xslt.Load (new XmlTextReader (new StringReader (
@"
")));
StringWriter sw = new StringWriter ();
xslt.Transform (new XPathDocument (new StringReader (
@"
1
2
3
4
")), null, new XmlTextWriter (sw));
Assert.AreEqual ("1234", sw.ToString ());
}
[Test]
public void CopyOfIXPathNavigable ()
{
string xsl = @"
";
string xml = @"
Fred
Freddy
Smith
Robert
Bob
Smith
";
XslTransform t = new XslTransform ();
t.Load (new XPathDocument (new StringReader (xsl)));
XsltArgumentList args = new XsltArgumentList();
args.AddExtensionObject ("urn:Foo", new CopyOfIXPathNavigableClass ());
StringWriter sw = new StringWriter ();
t.Transform (new XPathDocument (new StringReader (xml)), args, new XmlTextWriter (sw));
Assert.AreEqual ("", sw.ToString ());
}
public class CopyOfIXPathNavigableClass
{
public object MyCustomFunction ()
{
XmlDocument doc = new XmlDocument ();
doc .LoadXml ("");
return doc.DocumentElement;
}
}
[Test] // bug #316238
public void ResolveVariableInXsltArgumentList ()
{
ResolveVariableInXsltArgumentList ("Test/XmlFiles/xsl/316238-1.xsl");
ResolveVariableInXsltArgumentList ("Test/XmlFiles/xsl/316238-2.xsl");
}
void ResolveVariableInXsltArgumentList (string file)
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("this is a test");
XslTransform transform = new XslTransform ();
transform.Load (file);
//Stream outputStream = Console.OpenStandardOutput();
XsltArgumentList xsltArgs = new XsltArgumentList ();
xsltArgs.AddParam ("test", "", "test");
xsltArgs.AddParam ("doc", "", doc.CreateNavigator().Select ("*"));
transform.Transform (new XmlDocument (), xsltArgs, TextWriter.Null);
}
[Test] // bug #378239
public void WhitespaceEntityInStylesheet ()
{
string xslt = @"
]>
&nl;Hello&nl;World
";
string expected = @"
Hello
World";
XslTransform t = new XslTransform ();
t.Load (new XPathDocument (new StringReader (xslt)));
StringWriter sw = new StringWriter ();
t.Transform (new XPathDocument (new StringReader ("")), null, sw);
Assert.AreEqual (expected, sw.ToString ());
}
[Test] // bug #556619
public void RejectCurrencySymbolAsNumber ()
{
XslTransform xslt = new XslTransform ();
StringWriter sw = new StringWriter ();
string s =
@"
YES
NO
$4$2
";
xslt.Load (new XmlTextReader (new StringReader (s)));
xslt.Transform (new XPathDocument (new StringReader (
@"
")), null, sw);
Assert.IsTrue (sw.ToString ().IndexOf ("NO") > 0);
}
[Test] // bug #349375
public void PreserveWhitespace ()
{
XslCompiledTransform xslt = new XslCompiledTransform ();
xslt.Load (new XmlTextReader (new StringReader (@"
")));
StringWriter sw = new StringWriter ();
xslt.Transform (new XmlTextReader (new StringReader (@"
1
")), null, sw);
Assert.AreEqual (@"1 ", sw.ToString ());
}
[Test] // reverse case of #349375
[Category ("MobileNotWorking")]
public void PreserveWhitespace2 ()
{
XslCompiledTransform xslt = new XslCompiledTransform ();
xslt.Load (new XmlTextReader (new StringReader (@"
")));
StringWriter sw = new StringWriter ();
xslt.Transform (new XmlTextReader (new StringReader (@"
1
")), null, sw);
// Assert.AreEqual (@"1", sw.ToString ());
Assert.AreEqual ("\r\n \r\n 1\r\n \r\n".Replace ("\r\n", Environment.NewLine), sw.ToString ());
}
[Test]
public void CompiledTransform_Fragment () // bug #391424
{
XslCompiledTransform xslTransform = new XslCompiledTransform ();
XsltSettings settings = new XsltSettings ();
settings.EnableDocumentFunction = true;
xslTransform.Load ("Test/XmlFiles/xsl/391424.xsl", settings, new XmlUrlResolver ());
StringWriter sw = new StringWriter ();
xslTransform.Transform (new XPathDocument ("Test/XmlFiles/xsl/391424.xml", XmlSpace.Preserve), null, sw);
Assert.AreEqual ("Document found", sw.ToString ());
}
[Test]
public void XslTextElement_PreservesWhitespace () // bug 450797
{
XslCompiledTransform xslt = new XslCompiledTransform ();
xslt.Load (new XmlTextReader (new StringReader (@"
")));
StringWriter sw = new StringWriter ();
xslt.Transform (new XmlTextReader (new StringReader (@"bar")), null, sw);
Assert.AreEqual (" ", sw.ToString ());
}
[Test]
[Category ("MobileNotWorking")]
public void Bug487065 ()
{
using (XmlReader input = GetInput ()) {
using (XmlWriter output = XmlWriter.Create (Path.Combine(Path.GetTempPath(), "out.xml"))) {
PreProcess (input, output);
Assert.IsTrue (valueHasBeenSet, "#1");
}
}
}
private static XmlReader GetInput ()
{
return XmlReader.Create (new StringReader ("value1"));
}
private void PreProcess (XmlReader input, XmlWriter output)
{
var xsltsettings = new XsltSettings (true, true);
var xsltpreprocess = new XslCompiledTransform (true);
var stream =
new StringReader (@"
");
using (XmlReader xsltreader = XmlReader.Create (stream))
xsltpreprocess.Load (xsltreader, xsltsettings, new XmlUrlResolver());
var xsltargs = new XsltArgumentList ();
// The XSLT calls extension functions in _current_env.
xsltargs.AddExtensionObject ("environment", this);
xsltpreprocess.Transform (input, xsltargs, output, null);
}
public void define_text_constant (string name, string value)
{
Assert.AreEqual("var1", name, "#a1");
Assert.AreEqual("value1", value, "#a2");
valueHasBeenSet = true;
}
private bool valueHasBeenSet;
[Test] // bug #4434
public void IncludeProcessStylesheet ()
{
string includedXsl = @"
";
string file = Path.Combine (Path.GetTempPath (), "include.xsl");
StreamWriter includedWriter = new StreamWriter (file);
includedWriter.WriteLine (includedXsl);
includedWriter.Close ();
XslCompiledTransform transform = new XslCompiledTransform ();
string xsl = @"
".Replace ("include.xsl", file);
XmlReader xslReader = XmlReader.Create (new StringReader (xsl));
transform.Load (xslReader);
XmlReader inputReader = XmlReader.Create (new StringReader (""));
var sw = new StringWriter ();
XmlWriter outputWriter = XmlWriter.Create (sw);
transform.Transform (inputReader, outputWriter);
outputWriter.Close ();
Assert.AreEqual ("", sw.ToString (), "#1");
}
}
}