Created
December 10, 2018 02:23
-
-
Save meziantou/2c377432b178ebab17a6802f189adce7 to your computer and use it in GitHub Desktop.
XSLT sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
var xsl = XDocument.Parse(@"<xsl:stylesheet version=""1.0"" | |
xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" | |
xmlns:msxsl=""urn:schemas-microsoft-com:xslt"" | |
xmlns:user=""http://dummy/ns""> | |
<msxsl:script language=""C#"" implements-prefix=""user""> | |
<![CDATA[ | |
public string Code() | |
{ | |
return DateTime.Now.ToString(); | |
} | |
]]> | |
</msxsl:script> | |
<xsl:template match=""/""> | |
<xsl:value-of select=""user:Code()""/> | |
</xsl:template> | |
</xsl:stylesheet>" | |
); | |
var myXslTrans = new XslCompiledTransform(enableDebug: true); | |
var settings = new XsltSettings(); | |
settings.EnableScript = true; | |
myXslTrans.Load(xsl.CreateReader(), settings, null); | |
var doc = XDocument.Parse("<doc></doc>"); | |
var reader = doc.CreateReader(); | |
var sb = new StringBuilder(); | |
var writer = XmlWriter.Create(sb, new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Fragment }); | |
myXslTrans.Transform(reader, writer); | |
Console.WriteLine(sb.ToString()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment