XslCompiledTransformTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using NUnit.Framework;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.XPath;
  5. using System.Xml.Xsl;
  6. namespace MonoTests.System.Xml.Xsl
  7. {
  8. [TestFixture]
  9. public class XslCompiledTransformTests
  10. {
  11. [Test]
  12. public void GlobalVariableReferencesAnotherGlobalVariable ()
  13. {
  14. string xsl = @"<xsl:stylesheet version='1.0'
  15. xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  16. <xsl:variable name='global2'><xsl:value-of select='root/@attr' /></xsl:variable>
  17. <xsl:variable name='global1'>
  18. <xsl:for-each select='//foo'>
  19. <xsl:if test='@attr = $global2'>
  20. <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
  21. </xsl:if>
  22. </xsl:for-each>
  23. </xsl:variable>
  24. <xsl:template match='/'>
  25. <root>
  26. <xsl:value-of select='$global1' />
  27. </root>
  28. </xsl:template>
  29. </xsl:stylesheet>";
  30. StringWriter sw = new StringWriter ();
  31. XslCompiledTransform t = new XslCompiledTransform ();
  32. t.Load (new XPathDocument (new StringReader (xsl)));
  33. t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root attr='B'><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
  34. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root>foo: B</root>", sw.ToString ());
  35. }
  36. [Test]
  37. public void MSXslNodeSetAcceptsNodeSet ()
  38. {
  39. string xsl = @"<xsl:stylesheet version='1.0'
  40. xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  41. <xsl:template match='/'>
  42. <root>
  43. <!-- msxsl:node-set() accepts a node set -->
  44. <xsl:for-each select='msxsl:node-set(root/foo)'>
  45. <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
  46. </xsl:for-each>
  47. </root>
  48. </xsl:template>
  49. </xsl:stylesheet>";
  50. StringWriter sw = new StringWriter ();
  51. XslCompiledTransform t = new XslCompiledTransform ();
  52. t.Load (new XPathDocument (new StringReader (xsl)));
  53. // should transform without an exception
  54. t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
  55. }
  56. [Test]
  57. public void MSXslNodeSetAcceptsEmptyString ()
  58. {
  59. string xsl = @"<xsl:stylesheet version='1.0'
  60. xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:msxsl='urn:schemas-microsoft-com:xslt'>
  61. <xsl:template match='/'>
  62. <root>
  63. <!-- msxsl:node-set() accepts an empty string -->
  64. <xsl:variable name='empty'></xsl:variable>
  65. <xsl:for-each select='msxsl:node-set($empty)'>
  66. <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
  67. </xsl:for-each>
  68. </root>
  69. </xsl:template>
  70. </xsl:stylesheet>";
  71. StringWriter sw = new StringWriter ();
  72. XslCompiledTransform t = new XslCompiledTransform ();
  73. t.Load (new XPathDocument (new StringReader (xsl)));
  74. // should transform without an exception
  75. t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
  76. }
  77. [Test]
  78. public void ValueOfElementWithInsignificantWhitespace ()
  79. {
  80. string xsl = @"<?xml version='1.0' encoding='utf-8'?>
  81. <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  82. <xsl:template match='/'>
  83. <root>
  84. <bar>
  85. <xsl:if test='root/@attr'>
  86. <xsl:value-of select='root/@attr'>
  87. </xsl:value-of>
  88. </xsl:if>
  89. </bar>
  90. <baz>
  91. <xsl:for-each select='root/foo'>
  92. <xsl:if test='position() != 1'>
  93. <xsl:text>,</xsl:text>
  94. </xsl:if>
  95. <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
  96. </xsl:for-each>
  97. </baz>
  98. </root>
  99. </xsl:template>
  100. </xsl:stylesheet>";
  101. StringWriter sw = new StringWriter ();
  102. XslCompiledTransform t = new XslCompiledTransform ();
  103. t.Load (new XmlTextReader(new StringReader(xsl)));
  104. t.Transform (new XPathDocument (new XmlTextReader (new StringReader ("<root attr='D'><foo attr='A'/><foo attr='B'/><foo attr='C'/></root>"))), null, sw);
  105. Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?><root><bar>D</bar><baz>foo: A,foo: B,foo: C</baz></root>", sw.ToString ());
  106. }
  107. [Test]
  108. public void ElementToAttribute ()
  109. {
  110. var xsl = @"<?xml version='1.0' encoding='utf-8'?>
  111. <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
  112. <xsl:output method='xml'/>
  113. <xsl:template match='/'>
  114. <Node>
  115. <xsl:attribute name='name'>
  116. <xsl:call-template name='makeName'>
  117. <xsl:with-param name='Name' select='Node/Name' />
  118. </xsl:call-template>
  119. </xsl:attribute>
  120. </Node>
  121. </xsl:template>
  122. <xsl:template name='makeName'>
  123. <xsl:param name='Name' />
  124. <xsl:value-of select='$Name' />
  125. </xsl:template>
  126. </xsl:stylesheet>";
  127. var t = new XslCompiledTransform ();
  128. t.Load (new XmlTextReader (new StringReader (xsl)));
  129. var source = "<?xml version='1.0' encoding='utf-8' ?><Node><Name>123</Name></Node>";
  130. var expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><Node name=\"123\" />";
  131. StringWriter sw = new StringWriter ();
  132. t.Transform (new XPathDocument (new XmlTextReader (new StringReader (source))), null, sw);
  133. Assert.AreEqual (expected, sw.ToString ());
  134. }
  135. }
  136. }