[xsl] preserve-space and strip-space?

Subject: [xsl] preserve-space and strip-space?
From: "Kirk Allen Evans" <kaevans@xxxxxxxxx>
Date: Mon, 17 Sep 2001 08:28:52 -0400
Maybe I am wrong on what strip-space and preserve-space are supposed to do.
Using the following XML document, I pad each element's values with spaces.

<?xml version="1.0" encoding="UTF-8" ?>
<links>
 <link>     testing pre-stripping</link>
 <link>testing post-stripping         </link>
 <link>         testing any stripping      </link>
</links>

Then, applying the stylesheet, I specify the "link" element is to be
stripped.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:strip-space elements="link" />
 <xsl:template match="/">
  <xsl:for-each select="links/link">
   <xsl:text>[</xsl:text>
   <xsl:value-of select="." />
   <xsl:text>]</xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

I would expect the result as
<?xml version="1.0" encoding="UTF-16"?>[testing pre-stripping][testing
post-stripping][testing any stripping]

Instead, the result is
<?xml version="1.0" encoding="UTF-16"?>[     testing pre-stripping][testing
post-stripping         ][         testing any stripping      ]

If stripping does not affect the elements' values, then what does the
whitespace preserve and stripping preserve or strip?  I thought that it
might be whitespace within the tag delimiters, so I used the following XML
doc.

<?xml version="1.0" encoding="UTF-8" ?>
<links>
 <link  >     testing pre-stripping</link   >
 <link   >testing post-stripping         </link   >
 <link   >         testing any stripping      </link   >
</links>

The following stylesheet explicitly preserves space, but seems to have no
effect.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:preserve-space elements="link" />
 <xsl:template match="/">
  <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="@* | node()">
  <xsl:copy>
   <xsl:apply-templates select="@* | node()" />
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>


The result of this transformation is the same as the input, but the space
within the element tags is gone, so preserve-space had no noticeable effect
here.

<?xml version="1.0" encoding="UTF-16"?><links>
 <link>     testing pre-stripping</link>
 <link>testing post-stripping         </link>
 <link>         testing any stripping      </link>
</links>

--
Kirk Allen Evans
http://www.vbdna.net/rant

God bless the USA




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


Current Thread