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