safright
safright

Reputation: 127

xsl:apply-templates with params - how to save formatting?

XSLT is used as a template engine. I have some structure of templates, which transfer data one from another with params:

<xsl:template match="/" mode="common-page">
    <xsl:param name="page-content"/>
    …
    <xsl:copy-of select="$page-content"/>
    …
</xsl:template>

and call that template:

<xsl:template match="/">
    <xsl:apply-templates select="." mode="common-page">
        <xsl:with-param name="page-content">
        <!--some html content with formatting-->
        <html>
            <body>
                <h1>Header</h1>
                <div>
                    <p>Text</p>
                </div>
            </body>
        </html>
        </xsl:with-param>
    </xsl:apply-templates>
</xsl:template>

In this case, formatting in html content remains, output is:

<html>
    <body>
        <h1>Header</h1>
        <div>
            <p>Text</p>
        </div>
    </body>
</html>

But if I try to specify match or select in <xsl:apply-templates> (/result/page instead of /) formatting wiped out. Param page-content is same:

<html><body><h1>Header</h1><div><p>Text</p></div></body></html>

Example of XML:

<result xmlns:xlink="http://www.w3.org/TR/xlink" module="content" method="content" system-build="21199" lang="ru" header="Main page" title="Main page" request-uri="/.xml" pageId="2">
    <meta>
        <keywords/>
        <description/>
    </meta>
    <user id="42" status="auth" login="admin" xlink:href="uobject://42" type="sv"/>
    <parents/>
    <page id="2" parentId="0" link="/" is-default="1" is-active="1" object-id="313" type-id="50" type-guid="content-page" update-time="1364964725" alt-name="index">
        <basetype id="27" module="content">Content page</basetype>
        <name>Main page</name>
        <properties>
            <group id="65" name="common">
                <title>Params</title>
                <property id="117" name="h1" type="string">
                    <title>H1</title>
                    <value>Main page</value>
                </property>
            </group>
        </properties>
    </page>
</result>

Upvotes: 0

Views: 427

Answers (1)

Bobulous
Bobulous

Reputation: 13169

If you're using XSLT 2.0 and you're trying to get a result document which is indented (with a tab indent for each inner level of the XML) then you should be able to add an xsl:output element to your xsl:stylesheet element, and specify that indenting should be enabled.

For instance, at the start of your xsl:stylesheet element, you could add the following:

<xsl:stylesheet>
    <xsl:output indent="yes" />
    <!-- rest of document -->
</xsl:stylesheet>

Also see the method attribute for the xsl:output element, which sets the defaults for common output types such as "xml", "html", and "xhtml".

Upvotes: 0

Related Questions