How to format/indent output of an XSL Transformation
Asked Answered
D

1

5

I am trying to output a fragment of html code. But I need it to be pretty-printed/indented. Is there any way to do this without using <xsl:text>&#xa;</xsl:text> and <xsl:text>&#9;</xsl:text>?

I have used the following line without any results.

<xsl:output method="html" indent="yes"/>

Follwoing is the c# code;

    XslCompiledTransform XSLT = new XslCompiledTransform();
    XSLT.Load(xslPath);

    using (XmlTextWriter writer = new XmlTextWriter(writePath, null))
    {
        if (isTopLevel)
        {
            XSLT.Transform(XMLDocumentForCurrentUser, writer);
        }
        else
        {
            XsltArgumentList xslArg = new XsltArgumentList();
            xslArg.AddParam("MenuIndex", "", menuIndex);
            XSLT.Transform(XMLDocumentForCurrentUser, xslArg, writer);
        }
    }
 // I write the output to file  
//All this works fine, only now I need the HTML to be readable (in the browser's view source or any notepad)

Does anybody know of a way to format(atleast indent) XSLT output?

Deprivation answered 29/11, 2010 at 12:38 Comment(5)
@conqenator: If you don't want to use the serialization parameter xsl:output/@indent (maybe because it's no good like in MSXSL) and you don't want to do it manualy, then there is no solution in the scope of XSLT, but because you put such restrictions.Rife
You will need to provide more information to allow us to understand why indent="yes" does not work for you. So which is your XSLT processor, how exactly do you run the transformation, what exactly do you transform to (e.g. a file?), how do you look at the transformation result?Hands
@Alejandro: I am ready to and have used xsl:output. Just that, it simply showed no results for me.Deprivation
@Alejandro: I have added the code that invokes the transformation.Deprivation
Good question, +1. See my answer for links to XSLT pretty-printing resources.Adventuress
H
7

Don't create your own XmlTextWriter if you want the XSLT processor to apply the xsl:output directive. Instead either write directly to a file or create an XmlWriter as follows:

using (XmlWriter result = XmlWriter.Create(writePath, XSLT.OutputSettings))
{
        if (isTopLevel)
        {
            XSLT.Transform(XMLDocumentForCurrentUser, result);
        }
        else
        {
            XsltArgumentList xslArg = new XsltArgumentList();
            xslArg.AddParam("MenuIndex", "", menuIndex);
            XSLT.Transform(XMLDocumentForCurrentUser, xslArg, result);
        }
}
Hands answered 29/11, 2010 at 13:48 Comment(2)
+1 For correct answer, besides missing explanation: The transformation is serializing correctly the output result, but the XmlWriter class is stripping white space only text node by default; you need to turn off this behavior by a parameter to the XmlWriter class.Rife
Thank you! I was at my wit's end trying to figure out why the output is not indented and this fixed it.Grata

© 2022 - 2024 — McMap. All rights reserved.