[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] plain txt output tips


Subject: Re: [xsl] plain txt output tips
From: Nathan Shaw <n8_shaw@xxxxxxxxx>
Date: Tue, 16 Apr 2002 10:17:13 -0700 (PDT)

I am doing all of the parsing using Saxon, so no Java
involved here (although there will be in the future,
so perhaps I will explore this option a bit).

Right now I am trying to use a recursive template that
 I swiped from IBM to take care of the line breaks. I
actually have it working somewhat.

<!-- call the line break template -->
<xsl:variable name="output">
	<xsl:call-template name="breakText">
		<xsl:with-param name="strMsg" select="$content" />
		<xsl:with-param name="numChars" select="80" />
	</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$output" />

<!-- setLineBreaks.xsl -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:variable name="EOL" select="'&#xA;'"/>
<xsl:variable name="BREAK" select="' '"/>

<!-- 
  Breaks msg into one or more lines. 
  Lines are broken at the numChars-th character, so
each line will
  have no more than (numChars-1) characters each. 
-->
<xsl:template name="breakText">
	<xsl:param name="strMsg"/>
	<xsl:param name="numChars"/>
	<xsl:choose>
		<xsl:when test="string-length($strMsg) &lt;
$numChars">
			<xsl:value-of select="concat($strMsg,'++')"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="strFirst">
				<xsl:call-template
name="maxSubstringEndingWithBreak">
					<xsl:with-param name="strFragment"
select="substring($strMsg,1,$numChars)"/>
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="strRest"
select="substring-after($strMsg,$strFirst)"/>
			<xsl:value-of select="$strFirst"/>
			<xsl:value-of select="$EOL"/>
			<xsl:call-template name="breakText">
				<xsl:with-param name="strMsg" select="$strRest"/>
				<xsl:with-param name="numChars"
select="$numChars"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- 
  Given parameter s, this function returns the maximal
left 
  substring of s ending in a break character. If there
is no 
  break character, then the entire string is returned.
-->
<xsl:template name="maxSubstringEndingWithBreak">
	<xsl:param name="strFragment"/>
	<xsl:variable name="len"
select="string-length($strFragment)"/>
	<xsl:choose>
		<xsl:when test="len &lt;= 1 or
substring($strFragment,$len)=$BREAK or
contains($strFragment,$BREAK)=false">
			<xsl:value-of select="$strFragment"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template
name="maxSubstringEndingWithBreak">
				<xsl:with-param name="strFragment"
select="substring($strFragment, 1, $len - 1)"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>

Thanks for the tip on using templates instead of
value-of. I can see now how that is more efficient.

--Nate


--- mjyoungblut@xxxxxxx wrote:
> 
> Nate,
>       Are you using an OutputFormat, by chance? 
> That is, in Java, a
> org.apache.xml.serialize.OutputFormat?  We were
> using this an noticed that
> we were getting lines wrapped at 72 characters.  If
> so, just call the
> following method on the OutputFormat:
>       setLineWidth(0);        // No wrapping.  The
> default should be 0, but
> it appears to default to 72
> 
> Another suggestion - instead of traversing the tree
> like you have done
> below
>       PressRelease -> MetaData -> InstitutionalPAO
> -> xxx
> 
> you may just want to do the following:
> <xsl:apply-templates
> select="PressRelease/MetaData/InstitutionalPAO"/>
> 
> <xsl:template match="InstitutionalPAO">
>       <xsl:value-of
> select="FName"/><xsl:text>
</xsl:text>
>       <xsl:value-of
> select="LName"/><xsl:text>
</xsl:text>
>       ...
> </xsl:template>
> 
> Matt
> 
> 
> 
> 
> <!--contact info -->
> <xsl:value-of
>
select="PressRelease/MetaData/InstitutionalPAO/FName"
> /><xsl:text> </xsl:text><xsl:value-of
>
select="PressRelease/MetaData/InstitutionalPAO/LName"
> />
> <xsl:text>
</xsl:text>
> <xsl:value-of
>
select="PressRelease/MetaData/InstitutionalPAO/JobTitle"
> />
> <xsl:value-of
>
select="PressRelease/MetaData/InstitutionalPAO/OrgName"
> />
> <xsl:value-of
>
select="PressRelease/MetaData/InstitutionalPAO/Address"
> />
> <xsl:value-of
>
select="PressRelease/MetaData/InstitutionalPAO/SubAddress"
> />
> 
> But, I am not sure exactly how to send that variable
> into another named recursive template and spit out
> lines no greater than 72 characters long.
> 
> Could you perhaps help get me started here? I would
> appreciate it.
> 
> --Nate
> 
> 
> 
> 
> 
>  XSL-List info and archive: 
> http://www.mulberrytech.com/xsl/xsl-list
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread