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

Subject: Re: [xsl] Preserving indentation of first tag when copying XML into HTML?
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 21 Sep 2006 12:13:57 -0400
Roger,

It appears to me that the solution here lies in diagnosing exactly why the indentation isn't happening where you want it.

This is undoubtedly related to why it is appearing where you do want it.

There are generally two possibilities: either Evan's code is copying it from your input, or it is providing its own indenting. You need to look and see which it is.

Then you need to establish whether it is doing this correctly for the first node or not: check the HTML result to see this. It's possible that it is, but that your browser's handling of the pre element is suppressing it.

That will probably lead you to a reasonable solution.

Assuming your browser is behaving the way you want it to with the text node contents (the semantics of pre being what they are), you could try, instead of copying "parent::*[1]/child::*[1]/text()", just copy "text()[last()]" on the assumption that you want the first tag (the start tag of your code bit) to be indented the same distance as the last tag (the end tag).

Cheers,
Wendell

At 11:49 AM 9/21/2006, you wrote:
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