Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format/indent output of an XSL Transformation

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?

like image 800
Robin Maben Avatar asked Nov 29 '10 12:11

Robin Maben


People also ask

Is there a way to format indent in XSLT output?

Does anybody know of a way to format (atleast indent) XSLT output? @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.

What is the output of XSLT transformation?

The resulting output from the XSLT transformation is displayed in a new document window. The Output property specifies the filename for the output. If the Output property is blank, a filename is generated in your temporary directory. The file extension is based on the xsl:output element in your style sheet and can be . xml, . txt or . htm.

How do I convert an XML file to XSLT format?

Open an XML document in the XML editor. Associate an XSLT style sheet with the XML document. Add an xml-stylesheet processing instruction to the XML document. For example, add the following line to the document prolog: <?xml-stylesheet type='text/xsl' href='filename.xsl'?> Add the XSLT style sheet using the Properties window.

How do I use XSLT in Visual Studio Code?

Applies to: Visual Studio Visual Studio for Mac Visual Studio Code The XML editor lets you associate an XSLT style sheet with an XML document, perform the transformation, and view the output. The resulting output from the XSLT transformation is displayed in a new document window. The Output property specifies the filename for the output.


Video Answer


1 Answers

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);
        }
}
like image 148
Martin Honnen Avatar answered Sep 21 '22 00:09

Martin Honnen