xsl-list
[Top] [All Lists]

Re: Limit the length of transformed content from source text nodes

2005-10-25 09:37:39

if you need to snip at arbitrary depth it gets harder, this just adds
complete elements until you are too long or snips top level text

==============

<foo>
<item>This is the <strong>body</strong> of an article interspersed with 
<strong>markup tags</strong> I'm striving to snip.</item>
</foo>



==============

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
 version="1.0">

<xsl:variable name="tl" select="26"/>

<xsl:template match="foo">
<snippets>
<xsl:apply-templates/>
</snippets>
</xsl:template>

 
<xsl:template match="item">
<snippet>
<xsl:apply-templates mode="s" select="node()[1]"/>
</snippet>
</xsl:template>

<xsl:template match="strong">
<i>
<xsl:apply-templates/>
</i>
</xsl:template>

 
<xsl:template match="*" mode="s">
<xsl:param name="l" select="0"/>
<xsl:variable name="x">
<xsl:apply-templates select="."/>
</xsl:variable>
<xsl:variable name="xl" select="string-length($x)"/>
<xsl:choose>
<xsl:when test="$l + $xl &lt;= $tl">
 <xsl:copy-of select="$x"/>
  <xsl:apply-templates select="following-sibling::node()[1]" mode="s">
    <xsl:with-param name="l" select="$l+$xl"/>
  </xsl:apply-templates>
</xsl:when>
<xsl:otherwise>[...]</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="text()" mode="s">
<xsl:param name="l" select="0"/>

<xsl:variable name="xl" select="string-length(.)"/>
<xsl:choose>
<xsl:when test="$l + $xl &lt;= $tl">
 <xsl:value-of select="."/>
  <xsl:apply-templates select="following-sibling::node()[1]" mode="s">
    <xsl:with-param name="l" select="$l+$xl"/>
  </xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(.,1,$l + $tl - $xl)"/>[...]</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>



==============

$ saxon snip.xml snip.xsl
<?xml version="1.0" encoding="utf-8"?><snippets>
<snippet>This is the <i>body</i> of an a[...]</snippet>
</snippets>

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

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