[xsl] Preserving indentation of first tag when copying XML into HTML?

Subject: [xsl] Preserving indentation of first tag when copying XML into HTML?
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Thu, 21 Sep 2006 11:49:55 -0400
Hi Folks,

I am using Evan Lenz' stylesheet to copy XML into HTML.  I am embedding
the XML within an HTML <pre> element so that it retains all of its
formatting.

Everything works fine, except I am having a problem getting the start
tag of the first XML element to be indented properly (lined up with its
end tag).

Consider this XML of a Fitness Center:

<?xml version="1.0"?>
<FitnessCenter>
        <Member level="platinum">
                <Name>Jeff</Name>
                <Phone type="home">555-1234</Phone>
                <Phone type="work">555-4321</Phone>
                <FavoriteColor>lightgrey</FavoriteColor>
        </Member>
        <Member level="gold">
                <Name>David</Name>
                <Phone type="home">383-1234</Phone>
                <Phone type="work">383-4321</Phone>
                <FavoriteColor>lightblue</FavoriteColor>
        </Member>
        <Member level="platinum">
                <Name>Roger</Name>
                <Phone type="home">888-1234</Phone>
                <Phone type="work">888-4321</Phone>
                <FavoriteColor>lightyellow</FavoriteColor>
        </Member>
</FitnessCenter>

Let's suppose that I want to copy Member[1] and embed it within HTML.
Here's a template to do that:

    <xsl:template match="Member">
           <pre>
               <xsl:call-template name="xml-to-string">
                   <xsl:with-param name="node-set" select="."/>
               </xsl:call-template>
           </pre>
    </xsl:template>

(xml-to-string is Evan Lenz' named template.)

Here is how it appears in a browser:

<Member level="platinum">
                <Name>Jeff</Name>
                <Phone type="home">555-1234</Phone>
                <Phone type="work">555-4321</Phone>
                <FavoriteColor>lightgrey</FavoriteColor>
        </Member>

Notice that the indentation for the Member start tag has disappeared.

I want each element's start tag and end tag to line up.  I want it to
look like this in the browser:

        <Member level="platinum">
                <Name>Jeff</Name>
                <Phone type="home">555-1234</Phone>
                <Phone type="work">555-4321</Phone>
                <FavoriteColor>lightgrey</FavoriteColor>
        </Member>

Okay, so I thought, "let me obtain the whitespace between the parent
tag and the Member start tag".  Here's what I tried:

    <xsl:template match="Member">
           <pre>
               <xsl:value-of disable-output-escaping="yes"
select="parent::*[1]/child::*[1]/text()"/>
               <xsl:call-template name="xml-to-string">
                   <xsl:with-param name="node-set" select="."/>
               </xsl:call-template>
           </pre>
    </xsl:template>

Here is how it appears in a browser:

                <Member level="platinum">
                <Name>Jeff</Name>
                <Phone type="home">555-1234</Phone>
                <Phone type="work">555-4321</Phone>
                <FavoriteColor>lightgrey</FavoriteColor>
        </Member>

Notice that now the Member start tag is indented too much.

Can someone tell me how to do this so that the indentation of the first
tag is the same as it is in the instance document?

Thanks.  /Roger

Current Thread