Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT Transform not indenting properly

Here is an XSLT:

<xsl:stylesheet version="1.0" xmlns:P="http://abc.com/Xyz.xsd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="Thing">
        <xsl:element name="div">
            <xsl:attribute name="class">
                <xsl:text>Field</xsl:text>
            </xsl:attribute>

            <xsl:element name="span">
                <xsl:attribute name="class">
                    <xsl:text>Label</xsl:text>
                </xsl:attribute>

                <xsl:value-of select="$displayName"/>
                <xsl:text>:</xsl:text>
            </xsl:element>
            <xsl:element name="span">
                <xsl:attribute name="class">
                    <xsl:text>Input</xsl:text>
                </xsl:attribute>
            </xsl:element>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Here is the output of the XSLT Transformation:

<div class="Field"><span class="Label">Name:</span><span class="Input"></span></div>

Here is how I'm doing the transformation:

XslCompiledTransform xslTransform = new XslCompiledTransform();

xslTransform.Load(xsltPath, new XsltSettings(true, true), new XmlUrlResolver());

using (FileStream outputStream = File.Create(outputPath))
{
    using (StringReader stringReader = new StringReader(xml))
    {
        using (XmlReader xmlReader = XmlReader.Create(stringReader))
        {
            xslTransform.Transform(xmlReader, outputStream);
        }
    }
}

Why is the format not indented? Later on in the output, some things are indented. Not sure why. I'm looking for a solution that will honor the format settings as specified in the XSLT. This code is used to write to any format (XML, HTML, text, etc.) so I don't want specific code that will only work with XML, for example. But if my XSLT has an output of XML and is set to indent, then that should be honored.

like image 958
Josh M. Avatar asked May 08 '11 05:05

Josh M.


1 Answers

By default XmlWriter (which here is being used implicitly by XslCompiledTransform) does not indent your xml, and won't automatically use the settings speicifed in your xslt.

You can either explicitly supply settings to an XmlWriter that specify that the output can be indented, or the better approach is to have XmlWriter use the settings supplied by the xslt:

XslCompiledTransform xslTransform = new XslCompiledTransform();
xslTransform.Load(xsltPath, new XsltSettings(true, true), new XmlUrlResolver());

using (XmlWriter writer = XmlWriter.Create(outputPath, xslTransform.OutputSettings))
{
    using (StringReader stringReader = new StringReader(xml))
    {
        using (XmlReader xmlReader = XmlReader.Create(stringReader))
        {
            xslTransform.Transform(xmlReader, writer);
        }
    }
}
like image 61
Justin Avatar answered Sep 20 '22 14:09

Justin