Hi,
First of all I want to thank XmlLab for the nxslt2 too, great.
I’ve made two little document enhancements that i would like to share.
1) There is a little bug in the href attribute, namely, you can’t combine a combination of parameters, variables and constants the it. This comes because the href value is setted to the last part set to is. i.e. when you try something like href="{$prefix}{@Name}.cs", the href will be “.cs”.
2) I want to use nxslt2 for code generation, this means that I would like to generate files only once (optionally of coarse), therefore I introduced a ‘writeonce’ attribute.
We now can use the document function as follows:
<xsl:param name="prefix" /> <!— value = ‘c:\xxx\’ -->
…
<xsl:template match="Table">
<exsl:document href="{$prefix}{@Name}.cs" method="text" encoding="ascii" writeonce="true" >// <xsl:value-of select="@Name"/>.cs
to create “C:\xxx\Person.cs
In MultiXmlTextWriter.cs
public override void WriteString(string text)
{
//Possible exsl:document's attribute value
if (redirectState == RedirectState.WritingRedirectElementAttrValue)
switch (currentAttributeName)
case "href":
// add multiple parts to the href.
// href will be cleaned after the creation of the output file.
state.Href += text;
break;
case "writeonce":
// new writeonce attribute
state.WriteOnce = text.ToLower() == "true";
In OutputState.cs
// replace the InitWriter
public void InitWriter()
// Save current directory
storedDir = Directory.GetCurrentDirectory();
FileInfo file = new FileInfo(href);
DirectoryInfo dir = Directory.GetParent(href);
if (!dir.Exists)
dir.Create();
}
// Create writer
if (method == OutputMethod.Xml)
if (file.Exists && writeOnce)
// dummy writer
xmlWriter = new XmlTextWriter(new MemoryStream(), encoding);
else
xmlWriter = new XmlTextWriter(href, encoding);
if (indent)
xmlWriter.Formatting = Formatting.Indented;
if (!omitXmlDecl)
if (standalone)
xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartDocument();
textWriter = new StreamWriter(new MemoryStream(), encoding);
textWriter = new StreamWriter(href, false, encoding);
href = ""; // clean the href for the next usage
writeOnce = false; // clean the writeonce flage for the next usage
// Set new current directory
Directory.SetCurrentDirectory(dir.ToString());
// Add the next members
bool writeOnce = false;
public bool WriteOnce
get
return writeOnce;
set
writeOnce = value;
I Later desided I could use a BogusStream in stead of a MemoryStream, Just derive a class from Stream, remove the exception throwings, let it be capable of writing (not reading and seeking) and forget about the written data. This way it will work faster and it will use less memory.
Bye,
Karijn
Oops, now I'm the one that makes bugs.
in WriteString(string text)
we shouldn't add the line
href = "";
just remove it and it should work correctly.
Bye again.
PS. can somebody test this?