Re: [xsl] inserting structure

Subject: Re: [xsl] inserting structure
From: Manfred Staudinger <manfred.staudinger@xxxxxxxxx>
Date: Tue, 5 Jul 2005 23:31:21 +0200
> (it would be easier to understand if your variable names were more
> meaningful than A and B).
done

> Note that you can usually rewrite
>
> <xsl:element name="p">
> ...
> </xsl:element>
>
> as
>
> <p>
> ...
> </p>
done

> > > > Also the recursive template prevents me from adding html
> > header information.
> > I still don't know how to do this.
>
> I would write this as
>
> <xsl:template match="/">
>   <html><head>...</head>
>   <body>
>     <xsl:call-template name="replaceLineBreaks">
>       <xsl:with-param name="string" select="string(doc)"/>
>     </xsl:call-template>
>   </body>
>   </html>
> </xsl:template>
>
> <xsl:template name="replaceLineBreaks">
>   ...
> </xsl:template>
Thanks for this, I didn't really understand named templates.

After some trial I decided to
- split up the template into 3: processInputLines, insertParagraphs,
  and insertParagraphLines
- retain LF (#xA) throughout the process
- rely on concat and use string-length=0 as a switch
- use the first parameter for input, the second for passing on
- make the second parameter optional and give it a default length 0

I would appretiate your comments very much.

Thank you, Manfred Staudinger


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
              xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method = "html" omit-xml-declaration="yes" />

<xsl:template match="/">
<html lang="de" xml:lang="de">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Zeitungsbericht</title>
<style type="text/css">
body {
   font-family: Verdana, sans-serif;
}
blockquote {
   margin: 0 0 0 2em;
}
p {
   text-indent: 2em;
   margin: 0;
}
blockquote > p:first-child {
   text-indent: 0
}
</style>
</head>
<body>
   <xsl:call-template name="processInputLines">
      <xsl:with-param name="string" select="string(doc)"/>
   </xsl:call-template>
</body>
</html>
</xsl:template>

<xsl:template name="insertParagraphLines">
   <xsl:param name="paragraph" />
         <xsl:choose>
            <xsl:when test="starts-with($paragraph, '   ')">
               <xsl:value-of select="substring-before($paragraph, '&#xA;')"
/>
            </xsl:when>
            <xsl:when test="starts-with($paragraph, 'fol.')">
               <xsl:text>[</xsl:text>
               <xsl:value-of select="substring-before($paragraph, '&#xA;')"
/>
               <xsl:text>]</xsl:text>
            </xsl:when>
         </xsl:choose>
         <xsl:choose>
            <xsl:when test="string-length($paragraph) > 0">
               <xsl:call-template name="insertParagraphLines">
                  <xsl:with-param name="paragraph"
select="substring-after($paragraph, '&#xA;')" />
               </xsl:call-template>
            </xsl:when>
         </xsl:choose>
</xsl:template>

<xsl:template name="insertParagraphs">
   <xsl:param name="quote" />
   <xsl:param name="paragraph" select="''" />
         <xsl:choose>
            <xsl:when test="string-length($quote)=0 and
string-length($paragraph)=0">
            </xsl:when>
            <xsl:when test="string-length($paragraph)=0 or
	     not(string-length($quote)=0 or string-length($paragraph)=0 or
starts-with($quote, '      ') or starts-with($quote, '&#xA;'))">
               <xsl:call-template name="insertParagraphs">
                  <xsl:with-param name="quote"
select="substring-after($quote, '&#xA;')" />
                  <xsl:with-param name="paragraph"
		  select="concat($paragraph, substring-before($quote, '&#xA;'), '&#xA;')"
/>
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="string-length($paragraph) > 0">
               <p>
               <xsl:call-template name="insertParagraphLines">
                  <xsl:with-param name="paragraph" select="$paragraph" />
               </xsl:call-template>
               </p>
               <xsl:call-template name="insertParagraphs">
                  <xsl:with-param name="quote" select="$quote" />
               </xsl:call-template>
            </xsl:when>
         </xsl:choose>
 </xsl:template>

<xsl:template name="processInputLines">
<xsl:param name="string" />
<xsl:param name="quote" select="''" />
   <xsl:choose>
      <xsl:when test="starts-with($string, '&#xA;') and
string-length($quote)=0">
         <xsl:call-template name="processInputLines">
            <xsl:with-param name="string"
select="substring-after($string, '&#xA;')" />
         </xsl:call-template>
      </xsl:when>
      <xsl:when test="starts-with($string, ' ') or
starts-with($string, 'fol.')
	    or (starts-with($string, '&#xA;') and string-length($quote)>0)">
         <xsl:call-template name="processInputLines">
            <xsl:with-param name="string"
select="substring-after($string, '&#xA;')" />
            <xsl:with-param name="quote" select="concat($quote,
substring-before($string, '&#xA;'), '&#xA;')" />
         </xsl:call-template>
      </xsl:when>
      <xsl:when test="string-length($quote) > 0">
         <blockquote>
         <xsl:call-template name="insertParagraphs">
            <xsl:with-param name="quote" select="$quote" />
         </xsl:call-template>
         </blockquote>
         <xsl:call-template name="processInputLines">
            <xsl:with-param name="string" select="$string" />
         </xsl:call-template>
      </xsl:when>
      <xsl:when test="string-length($string) = 0">
      </xsl:when>
      <xsl:otherwise>
         <div><xsl:value-of select="substring-before($string, '&#xA;')"
/></div>
         <xsl:call-template name="processInputLines">
            <xsl:with-param name="string"
select="substring-after($string, '&#xA;')" />
         </xsl:call-template>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>
</xsl:stylesheet>

Current Thread