Altova Mailing List Archives


Re: [xsl] question on apply-templates and strip-space

From: Peter Davis <pdavis152@--------->
To:
Date: 2/20/2002 3:25:00 PM
strip-space only removes text nodes that only contain whitespace.  The text 
nodes in your file that are being output begin and end with whitespace, but 
do not contain only whitespace, so they are not trimmed.  You are correct 
that normalize-space would fix the problem.

Since you don't have a template for Tag1 explicitely, it is using the default 
template to output the text of Tag1 (as I'm sure you are aware).  What you 
can do is create a template that matches the text:

<xsl:template match="text()">
  <!-- you can also match Tag1/text() if you don't want to trim spaces
       in other tags -->
  <xsl:value-of select="normalize-space(.)"/>
</xsl:template>

Hopefully this will not create problems by stripping too much whitespace, 
ie., the spaces before and after <Tag2></Tag2>.  If it does, you could output 
those spaces with <xsl:text> {</xsl:text> and <xsl:text> }</xsl:text> at the 
beginning and end of the Tag2 template.

On Wednesday 20 February 2002 16:55, Alice Tull wrote:
> Hi,
>
> I'm new to XSL. I'm trying to get the following input:
>
> 	<MyStuff>
> 	     <Tag1>
> 		This is just a <Tag2>normal</Tag2> line.
> 	     </Tag1>
> 	     <Tag1>
> 		This is the second <Tag2>normal</Tag2> line.
> 	     </Tag1>
> 	</MyStuff>
>
> To be generated as the following output:
>
> 	Ta-Da:This is just a {1} line.
> 	Ta-Da:This is the second {1} line.
>
> I want the result to be simple "text" rather than XML or HTML.
>
> Here's my stylesheet:
>
> 	<xsl:stylesheet version="1.0"
> 	 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> 	   <xsl:output method="text" indent="no" />
>
> 	   <xsl:strip-space elements="*"/>
>
> 	   <xsl:template match="Tag2">{<xsl:number count="*"/>}</xsl:template>
>
> 	   <xsl:template match="/">
> 	    <xsl:for-each select="MyStuff">
> 	      <xsl:text>Ta-Da:</xsl:text>
> 	      <xsl:apply-templates select="Tag1"/><xsl:text>&#xA;</xsl:text>
> 	    </xsl:for-each>
> 	   </xsl:template>
> 	</xsl:stylesheet>
>
> My output was:
> 	Ta-Da:
> 	   This is just a {1} line.
>
> 	Ta-Da:
> 	   This is the second {1} line.
>
> Which has more spaces and linefeeds than I want.
> The spaces in the text output was exactly the same as the spaces in
> the original xml file. Shouldn't "strip-space" get rid of the
> linefeed and spaces in the original <Tag1> node? Will normalize-space
> help, and how to use it in the case where I need to apply-templates?
>
> Btw, I tried this on the latest version on both Xalan and libxslt.
>
> thanx,
> alice
>
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

-- 
Peter Davis
	"Are you police officers?"
	"No, ma'am.  We're musicians."
		-- The Blues Brothers

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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.