4 using System.Xml.XPath;
7 namespace MonoTests.System.Xml.Xsl
10 public class XslCompiledTransformTests
13 public void GlobalVariableReferencesAnotherGlobalVariable ()
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' />
25 <xsl:template match='/'>
27 <xsl:value-of select='$global1' />
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 ());
39 public void MSXslNodeSetAcceptsNodeSet ()
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='/'>
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' />
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);
60 public void MSXslNodeSetAcceptsEmptyString ()
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='/'>
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' />
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);
82 public void ValueOfElementWithInsignificantWhitespace ()
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='/'>
89 <xsl:if test='root/@attr'>
90 <xsl:value-of select='root/@attr'>
95 <xsl:for-each select='root/foo'>
96 <xsl:if test='position() != 1'>
97 <xsl:text>,</xsl:text>
99 <xsl:value-of select='name(.)' />: <xsl:value-of select='@attr' />
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 ());
113 public void ElementToAttribute ()
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='/'>
120 <xsl:attribute name='name'>
121 <xsl:call-template name='makeName'>
122 <xsl:with-param name='Name' select='Node/Name' />
128 <xsl:template name='makeName'>
129 <xsl:param name='Name' />
130 <xsl:value-of select='$Name' />
134 var t = new XslCompiledTransform ();
135 t.Load (new XmlTextReader (new StringReader (xsl)));
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\" />";
140 StringWriter sw = new StringWriter ();
141 t.Transform (new XPathDocument (new XmlTextReader (new StringReader (source))), null, sw);
142 Assert.AreEqual (expected, sw.ToString ());