| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- //
- // System.Xml.Xsl.XslTransformTests.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // (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 : Assertion
- {
- XmlDocument doc;
- XslTransform xslt;
- XmlDocument result;
- [SetUp]
- public void GetReady()
- {
- doc = new XmlDocument ();
- xslt = new XslTransform ();
- result = new XmlDocument ();
- }
- [Test]
- public void TestBasicTransform ()
- {
- doc.LoadXml ("<root/>");
- xslt.Load ("Test/XmlFiles/xsl/empty.xsl");
- xslt.Transform ("Test/XmlFiles/xsl/empty.xsl", "Test/XmlFiles/xsl/result.xml");
- result.Load ("Test/XmlFiles/xsl/result.xml");
- AssertEquals ("count", 2, result.ChildNodes.Count);
- }
- [Test]
- [ExpectedException (typeof (XsltCompileException))]
- public void InvalidStylesheet ()
- {
- XmlDocument doc = new XmlDocument ();
- doc.LoadXml ("<xsl:element xmlns:xsl='http://www.w3.org/1999/XSL/Transform' />");
- 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 xml = @"<root>text</root>";
- string xsl = @"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
- <xsl:template match='/root'>
- <xsl:call-template name='foo'>
- <xsl:with-param name='name' value='text()' />
- </xsl:call-template>
- </xsl:template>
- <xsl:template name='foo'>
- <xsl:param name='name' />
- <result>
- <xsl:if test='1'>
- <xsl:variable name='last' value='text()' />
- <xsl:value-of select='$last' />
- </xsl:if>
- </result>
- </xsl:template>
- </xsl:stylesheet>
- ";
- XslTransform xslt = new XslTransform ();
- xslt.Load (new XPathDocument (new XmlTextReader (xsl, XmlNodeType.Document, null)));
- }
- [Test()]
- [Category ("NotWorking")] // it depends on "mcs" existence
- public void MsxslTest() {
- string _styleSheet = @"
- <xslt:stylesheet xmlns:xslt=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"" xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" xmlns:stringutils=""urn:schemas-sourceforge.net-blah"">
- <xslt:output method=""text"" />
- <msxsl:script language=""C#"" implements-prefix=""stringutils"">
- <![CDATA[
- string PadRight( string str, int padding) {
- return str.PadRight(padding);
- }
- ]]>
- </msxsl:script>
- <xslt:template match=""test"">
- <xslt:value-of select=""stringutils:PadRight(@name, 20)"" />
- </xslt:template>
- </xslt: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("<test name=\"test\" />");
- // Do transformation
- transform.Transform(xmlDoc, new XsltArgumentList(), writer, new XmlUrlResolver());
- AssertEquals("test".PadRight(20), sb.ToString());
- }
- [Test]
- public void MSXslNodeSet ()
- {
- string xsl = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
- <xsl:template match='/'>
- <root>
- <xsl:variable name='var'>
- <xsl:copy-of select='root/foo' />
- </xsl:variable>
- <xsl:for-each select='msxsl:node-set($var)/foo'>
- <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
- </xsl:for-each>
- </root>
- </xsl:template>
- </xsl:stylesheet>";
- StringWriter sw = new StringWriter ();
- XslTransform t = new XslTransform ();
- t.Load (new XPathDocument (new StringReader (xsl)));
- t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
- AssertEquals (@"<?xml version=""1.0"" encoding=""utf-16""?><root xmlns:msxsl=""urn:schemas-microsoft-com:xslt"">foo: Afoo: Bfoo: C</root>", sw.ToString ());
- }
- [Test]
- [Category ("NotDotNet")]
- // Actually MS.NET here throws XsltException, but Mono returns
- // XPathException (since XPath evaluation engine generally
- // catches (should catch) static error. It is implementation
- // dependent matter.
- [ExpectedException (typeof (XPathException))]
- public void MSXslNodeSetRejectsNodeSet ()
- {
- string xsl = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
- <xsl:template match='/'>
- <root>
- <!-- msxsl:node-set() does not accept a node set -->
- <xsl:for-each select='msxsl:node-set(root/foo)'>
- <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
- </xsl:for-each>
- </root>
- </xsl:template>
- </xsl:stylesheet>";
- StringWriter sw = new StringWriter ();
- XslTransform t = new XslTransform ();
- t.Load (new XPathDocument (new StringReader (xsl)));
- t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
- }
- [Test]
- public void EvaluateEmptyVariableAsBoolean ()
- {
- string xsl = @"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
- <xsl:template match='/'>
- <xsl:variable name='var'><empty /></xsl:variable>
- <root><xsl:if test='$var'>true</xsl:if></root>
- </xsl:template>
- </xsl:stylesheet>";
- XslTransform t = new XslTransform ();
- t.Load (new XPathDocument (new StringReader (xsl)));
- StringWriter sw = new StringWriter ();
- t.Transform (
- new XPathDocument (new StringReader ("<root/>")),
- null,
- sw);
- Assert (sw.ToString ().IndexOf ("true") > 0);
- }
- [Test]
- [ExpectedException (typeof (XsltCompileException))]
- public void NotAllowedPatternAxis ()
- {
- string xsl = @"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
- <xsl:template match='/descendant-or-self::node()/elem'>
- <ERROR/>
- </xsl:template>
- </xsl:stylesheet>";
- new XslTransform ().Load (new XPathDocument (
- new StringReader (xsl)));
- }
- [Test]
- [ExpectedException (typeof (XsltCompileException))]
- public void ImportIncorrectlyLocated ()
- {
- string xsl = @"<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
- <xsl:template match='/'></xsl:template>
- <xsl:import href='dummy.xsl' />
- </xsl:transform>";
- new XslTransform ().Load (new XPathDocument (
- new StringReader (xsl)));
- }
- [Test]
- [Category ("NotDotNet")]
- public void DontHoldStylesheetReference ()
- {
- // This test is optional. Examines whether Load() does
- // not hold loaded stylesheet XPathNavigator internally.
- XslTransform t = new XslTransform ();
- WeakReference wr = StylesheetLoad (t, "<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'/>");
- GC.Collect (0);
- GC.Collect (2);
- Assert ("load", !wr.IsAlive);
- wr = StylesheetTransform (t, "<root/>");
- GC.Collect (0);
- GC.Collect (2);
- Assert ("transform", !wr.IsAlive);
- }
- 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 = @"<xsl:stylesheet
- xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
- version='1.0'>
- <xsl:variable name='n'
- select='document(""notexist.xml"")' />
- <xsl:template match='/'>xx</xsl:template>
- </xsl:stylesheet>";
- string xmlText = @"<root />";
- 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));
- transform.Transform (xmlDocument, null, TextWriter.Null);
- }
- }
- }
|