2
0

XslTransformTests.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // System.Xml.Xsl.XslTransformTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2002 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Text;
  13. using System.Xml;
  14. using System.Xml.XPath;
  15. using System.Xml.Xsl;
  16. using NUnit.Framework;
  17. namespace MonoTests.System.Xml.Xsl
  18. {
  19. [TestFixture]
  20. public class XslTransformTests : Assertion
  21. {
  22. XmlDocument doc;
  23. XslTransform xslt;
  24. XmlDocument result;
  25. [SetUp]
  26. public void GetReady()
  27. {
  28. doc = new XmlDocument ();
  29. xslt = new XslTransform ();
  30. result = new XmlDocument ();
  31. }
  32. [Test]
  33. public void TestBasicTransform ()
  34. {
  35. doc.LoadXml ("<root/>");
  36. xslt.Load ("Test/XmlFiles/xsl/empty.xsl");
  37. xslt.Transform ("Test/XmlFiles/xsl/empty.xsl", "Test/XmlFiles/xsl/result.xml");
  38. result.Load ("Test/XmlFiles/xsl/result.xml");
  39. AssertEquals ("count", 2, result.ChildNodes.Count);
  40. }
  41. [Test]
  42. [ExpectedException (typeof (XsltCompileException))]
  43. public void InvalidStylesheet ()
  44. {
  45. XmlDocument doc = new XmlDocument ();
  46. doc.LoadXml ("<xsl:element xmlns:xsl='http://www.w3.org/1999/XSL/Transform' />");
  47. XslTransform t = new XslTransform ();
  48. t.Load (doc);
  49. }
  50. [Test]
  51. [ExpectedException (typeof (XsltCompileException))]
  52. public void EmptyStylesheet ()
  53. {
  54. XmlDocument doc = new XmlDocument ();
  55. XslTransform t = new XslTransform ();
  56. t.Load (doc);
  57. }
  58. [Test]
  59. [ExpectedException (typeof (XsltCompileException))]
  60. public void InvalidStylesheet2 ()
  61. {
  62. string xml = @"<root>text</root>";
  63. string xsl = @"<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
  64. <xsl:template match='/root'>
  65. <xsl:call-template name='foo'>
  66. <xsl:with-param name='name' value='text()' />
  67. </xsl:call-template>
  68. </xsl:template>
  69. <xsl:template name='foo'>
  70. <xsl:param name='name' />
  71. <result>
  72. <xsl:if test='1'>
  73. <xsl:variable name='last' value='text()' />
  74. <xsl:value-of select='$last' />
  75. </xsl:if>
  76. </result>
  77. </xsl:template>
  78. </xsl:stylesheet>
  79. ";
  80. XslTransform xslt = new XslTransform ();
  81. xslt.Load (new XPathDocument (new XmlTextReader (xsl, XmlNodeType.Document, null)));
  82. }
  83. [Test()]
  84. public void MsxslTest() {
  85. string _styleSheet = @"
  86. <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"">
  87. <xslt:output method=""text"" />
  88. <msxsl:script language=""C#"" implements-prefix=""stringutils"">
  89. <![CDATA[
  90. string PadRight( string str, int padding) {
  91. return str.PadRight(padding);
  92. }
  93. ]]>
  94. </msxsl:script>
  95. <xslt:template match=""test"">
  96. <xslt:value-of select=""stringutils:PadRight(@name, 20)"" />
  97. </xslt:template>
  98. </xslt:stylesheet>";
  99. StringReader stringReader = new StringReader(_styleSheet);
  100. XslTransform transform = new XslTransform();
  101. XmlTextReader reader = new XmlTextReader(stringReader);
  102. transform.Load(reader, new XmlUrlResolver(), AppDomain.CurrentDomain.Evidence);
  103. StringBuilder sb = new StringBuilder();
  104. StringWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture);
  105. XsltArgumentList arguments = new XsltArgumentList();
  106. XmlDocument xmlDoc = new XmlDocument();
  107. xmlDoc.LoadXml("<test name=\"test\" />");
  108. // Do transformation
  109. transform.Transform(xmlDoc, new XsltArgumentList(), writer, new XmlUrlResolver());
  110. AssertEquals("test".PadRight(20), sb.ToString());
  111. }
  112. [Test]
  113. public void MSXslNodeSet ()
  114. {
  115. string xsl = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  116. <xsl:template match='/'>
  117. <root>
  118. <xsl:variable name='var'>
  119. <xsl:copy-of select='root/foo' />
  120. </xsl:variable>
  121. <xsl:for-each select='msxsl:node-set($var)/foo'>
  122. <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
  123. </xsl:for-each>
  124. </root>
  125. </xsl:template>
  126. </xsl:stylesheet>";
  127. StringWriter sw = new StringWriter ();
  128. XslTransform t = new XslTransform ();
  129. t.Load (new XPathDocument (new StringReader (xsl)));
  130. t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
  131. AssertEquals (@"<?xml version=""1.0"" encoding=""utf-16""?><root xmlns:msxsl=""urn:schemas-microsoft-com:xslt"">foo: Afoo: Bfoo: C</root>", sw.ToString ());
  132. }
  133. [Test]
  134. // Actually MS.NET here throws XsltException, but Mono returns
  135. // XPathException (since XPath evaluation engine generally catches
  136. // (should catch) this kind of error. It is implementation
  137. // dependent matter.
  138. [ExpectedException (typeof (XPathException))]
  139. public void MSXslNodeSetRejectsNodeSet ()
  140. {
  141. string xsl = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  142. <xsl:template match='/'>
  143. <root>
  144. <!-- msxsl:node-set() does not accept a node set -->
  145. <xsl:for-each select='msxsl:node-set(root/foo)'>
  146. <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
  147. </xsl:for-each>
  148. </root>
  149. </xsl:template>
  150. </xsl:stylesheet>";
  151. StringWriter sw = new StringWriter ();
  152. XslTransform t = new XslTransform ();
  153. t.Load (new XPathDocument (new StringReader (xsl)));
  154. t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
  155. }
  156. }
  157. }