Context before and after a word (xslt) -
<block> <p>(...) nogen forundring, med dyb bedrøvelse, men <seg>end</seg> dybere rolighed, læsde jeg baggesens svar til mig skilderiet no. 9 (...)</p> </block> i new when comes xslt. how pick context, 3 words before , after, content tagged element ? have been trying whit string-before , string-after whit no success @ all.
the result should this:
word: end context: dyb bedrøvelse, men end dybere rolighed, læsde
we not know whether xslt processor supports xslt 2.0, since wrote answer before realizing that:
the reason version important solution below uses function available in xslt 2.0, namely tokenize(). saying things "selecting 3 words before , after" make sense if tokenize strings want process in fashion. before tokenization, concept of "words" unknown xslt processor.
stylesheet
edit: response comment, have adapted code work several seg elements. of course, introduces other difficulties need deal with.
<?xml version="1.0" encoding="utf-8" ?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:output method="text"/> <xsl:template match="seg"> <xsl:text>word: </xsl:text> <xsl:value-of select="."/> <xsl:text>
context: </xsl:text> <xsl:variable name="tok-before" select="tokenize(normalize-space(string-join(preceding::text(),'')),' ')"/> <xsl:variable name="tok-after" select="tokenize(normalize-space(string-join(following::text(),'')),' ')"/> <xsl:value-of select="subsequence($tok-before,count($tok-before) -2)"/> <xsl:value-of select="concat(' ',.,' ')"/> <xsl:value-of select="subsequence($tok-after,1,3)"/> <xsl:text>
</xsl:text> </xsl:template> <xsl:template match="text()"/> </xsl:transform> xml output
word: end context: med dyb bedrøvelse, men end dybere rolighed, læsde you can try , manipulate solution online here.
Comments
Post a Comment