xsl-list
[Top] [All Lists]

Re: [xsl] Selecting 400 characters before a string such as YYY in a large document.

2010-06-13 04:57:43
I don't see the necessity for reversing the content. True, "greedy" matching
(implying backup) may not be the fastest way in some situations; as an
alternative there is "reluctant" matching (e.g. '.*?').

But if the YYY is a literal string (not a pattern) substring-before is
suffcient.

  <out1>
    <xsl:variable name="bef" select="substring-before(.,'YYY')"/>
    <xsl:value-of select="substring($bef,string-length($bef)-400+1,400)"/>
  </out1>

If there's the possibility that there aren't 400 characters before YYY,
add a choice.

Here is the solution with pattern matching:
  <out2>
    <xsl:variable name="bef" select="."/>
    <xsl:value-of select="replace($bef,'^.*?(.{400})YYY.*$', '$1')"/>
  </out2>
Here, using '.{1,400}' would be necessary to handle "short" cases.

-W



On 13 June 2010 10:11, Alex Muir <alex(_dot_)g(_dot_)muir(_at_)gmail(_dot_)com> 
wrote:

Hi,

I need to select 400 characters before a string such as YYY in a
larger text document.

I've in the past reversed the content to then find the 400 characters
before YYY using regex effectively however I'm wondering if there is a
better perhaps simpler way to do this in xslt?


Thanks


--
Alex

An informal recording with one mic under a tree leads to some pretty
sweet acoustic sounds.
https://sites.google.com/site/greigconteh/albums/diabarte-and-sons

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


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