Re: [xsl] plain txt output tips

Subject: Re: [xsl] plain txt output tips
From: Nathan Shaw <n8_shaw@xxxxxxxxx>
Date: Tue, 16 Apr 2002 12:12:13 -0700 (PDT)
I am getting closer, but am still having problems
trying to get the line breaks to behave.

Can anyone tell me why I am now getting output like
this, where some lines break correctly at 72 char and
other break earlier incorrectly.

------------------------------------------------------
SPACE SHUTTLE TO LAUNCH 
FIRST SPACE RAILROAD IN APRIL

Expanding the new frontier just as they 
did the old, railroads will take flight next month as
the first space 
railroad is launched aboard Space Shuttle Atlantis.

Circling Earth 
aboard the International Space Station, the car on
this railway will 
have a top speed of only 300 feet per hour, but the
entire line -- 
tracks and all -- will travel almost nine times faster
than a speeding 
bullet, over 17,000 miles per hour, in orbit. The rail
line eventually 
will stretch almost 100 yards along the structural
backbone of the 
station, serving as a mobile base from which the
station's 
Canadian-built robotic arm can assemble and maintain
the complex.

My 
UnOrdered List
* This is list item 1
* This is list item 2
* This is 
listitem 3

My Ordered List
1. This is list item 1
2. This is list item 
2
3. This is listitem 3
------------------------------------------------------

Here is the recursive template I am using. Strmsg is
coming in as a string holding the entire output and
numChars is set to 72.

------------------------------------------------------
<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="$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 in advance.

--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