Generating multiple files using XSLT 1.0
-
Hi I am doing a code generation app. using .Net 3.5. I wish to know if it is possible to generate multiple code files from a single xslt file. I know that it can be accomplished in XSLT 2.0 using result-document tag. Is it possible using XSLT 1.0 ? Thanks Fadi
-
Hi I am doing a code generation app. using .Net 3.5. I wish to know if it is possible to generate multiple code files from a single xslt file. I know that it can be accomplished in XSLT 2.0 using result-document tag. Is it possible using XSLT 1.0 ? Thanks Fadi
Exslt[^] provides the exsl:document[^] element - but that only seems to be implemented in libxslt. As you're using .NET, though, you could implement something like exsl:document using an Extension Object[^].
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Exslt[^] provides the exsl:document[^] element - but that only seems to be implemented in libxslt. As you're using .NET, though, you could implement something like exsl:document using an Extension Object[^].
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thank you Stuart But can you clarify some more things? My xml file is
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>My xslt file is
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:output="urn:output"><xsl:template match="bookstore">
<bookstore>
<xsl:for-each select="book">
<book>
<xsl:copy-of select="node()"/>
<new-price>
<xsl:value-of select="output:SaveOutput(@genre, node())"/>
</new-price>
</book>
</xsl:for-each>
</bookstore>
</xsl:template>
</xsl:stylesheet>C# code goes like
private void ExtensionTest()
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("extension.xslt");
XsltArgumentList xslArg = new XsltArgumentList();OutputSaver obj = new OutputSaver(); xslArg.AddExtensionObject("urn:output", obj); using (XmlWriter w = XmlWriter.Create("output.xml")) { xslt.Transform("books.xml", xslArg, w); }
}
public class OutputSaver
{
public void SaveOutput(string fileName, string node)
{
StreamWriter s = new StreamWriter(fileName);
s.Write(node);
s.Flush();
}
}Now I have 2 issues * How to pass the xslt node content to SaveOutput
-
Thank you Stuart But can you clarify some more things? My xml file is
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>My xslt file is
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:output="urn:output"><xsl:template match="bookstore">
<bookstore>
<xsl:for-each select="book">
<book>
<xsl:copy-of select="node()"/>
<new-price>
<xsl:value-of select="output:SaveOutput(@genre, node())"/>
</new-price>
</book>
</xsl:for-each>
</bookstore>
</xsl:template>
</xsl:stylesheet>C# code goes like
private void ExtensionTest()
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("extension.xslt");
XsltArgumentList xslArg = new XsltArgumentList();OutputSaver obj = new OutputSaver(); xslArg.AddExtensionObject("urn:output", obj); using (XmlWriter w = XmlWriter.Create("output.xml")) { xslt.Transform("books.xml", xslArg, w); }
}
public class OutputSaver
{
public void SaveOutput(string fileName, string node)
{
StreamWriter s = new StreamWriter(fileName);
s.Write(node);
s.Flush();
}
}Now I have 2 issues * How to pass the xslt node content to SaveOutput
I got one more query. Will it be possible by embedding script in xslt? I tried the following way Xslt file
?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:user="urn:myscripts"<xsl:output method="xml" indent="yes"/>
<msxsl:script language="C#" implements-prefix="user">
<![CDATA[
public void SaveOutput(string fileName)
{System.IO.TextWriter t = new System.IO.StreamWriter(fileName); t.Write("got it"); t.Flush(); } \]\]>
</msxsl:script>
<xsl:template match="@* | node()">
<xsl:value-of select="user:SaveOutput('d:\testing.txt')"/></xsl:template>
</xsl:stylesheet>
My C# code is
private void ScriptTest() { XsltSettings settings = new XsltSettings(); settings.EnableScript = true; XslCompiledTransform trs = new XslCompiledTransform(); trs.Load("Custom.xslt", settings, new XmlUrlResolver()); StreamWriter writer = new StreamWriter(@"e:\\samples.txt"); trs.Transform(@"E:\\\\9.xml", null, writer); writer.Flush(); writer.Close(); }
But when I run the code, I get en error like IOException unhandled - The device is not ready Thank you Fadi
-
I got one more query. Will it be possible by embedding script in xslt? I tried the following way Xslt file
?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:user="urn:myscripts"<xsl:output method="xml" indent="yes"/>
<msxsl:script language="C#" implements-prefix="user">
<![CDATA[
public void SaveOutput(string fileName)
{System.IO.TextWriter t = new System.IO.StreamWriter(fileName); t.Write("got it"); t.Flush(); } \]\]>
</msxsl:script>
<xsl:template match="@* | node()">
<xsl:value-of select="user:SaveOutput('d:\testing.txt')"/></xsl:template>
</xsl:stylesheet>
My C# code is
private void ScriptTest() { XsltSettings settings = new XsltSettings(); settings.EnableScript = true; XslCompiledTransform trs = new XslCompiledTransform(); trs.Load("Custom.xslt", settings, new XmlUrlResolver()); StreamWriter writer = new StreamWriter(@"e:\\samples.txt"); trs.Transform(@"E:\\\\9.xml", null, writer); writer.Flush(); writer.Close(); }
But when I run the code, I get en error like IOException unhandled - The device is not ready Thank you Fadi
I remembered that there's an Exslt implementation for .NET[^] - you can use the exsl:document() support it contains[^]. Easier than trying to write your own...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p