xsl-list
[Top] [All Lists]

Re: [xsl] removing final space from node tree

2009-04-20 18:07:38
 <!-- We need to remove any extraneous final spaces. -->
 <!-- set context node to language-desc -->
 <xsl:for-each select="$language-desc">
   <!-- find last text node in descendants -->
   <xsl:variable name="last-text-node" select="(.//text())[last()]"/>
     <xsl:choose>
       <xsl:when test="ends-with($last-text-node, ' ')">
         <xsl:apply-templates mode="strip-final-space" select=".">
           <xsl:with-param name="last-text-node"
               select="$last-text-node"/>
           </xsl:apply-templates>
       </xsl:when>
       <xsl:otherwise><xsl:sequence select="."/></xsl:otherwise>
     </xsl:choose>
   </xsl:for-each>

...

   <!-- copy the context node tree, removing final space from the node
that is passed as a parameter. -->
   <xsl:template mode="strip-final-space" match="@*|node()">
       <xsl:param name="last-text-node" />
       <xsl:choose>
           <xsl:when test=". is $last-text-node"><xsl:value-of
select="substring(., 1, string-length(.) - 1)"/></xsl:when>
           <xsl:otherwise>
               <xsl:copy>
                   <xsl:apply-templates mode="strip-final-space"
select="@*|node()">
                       <xsl:with-param name="last-text-node"
select="$last-text-node"/>
                   </xsl:apply-templates>
               </xsl:copy>
           </xsl:otherwise>
       </xsl:choose>
   </xsl:template>


I think this would do the same job:

<xsl:template match="text()">
  <xsl:value-of select="if (. is
/path/to/lanuguage-desc/descendant::text()[last()][ends-with(., ' ')])
then
      substring(., 1, string-length(.) - 1) else ."/>
</xsl:template>



-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>