Context before and after a word (xslt)

让人想犯罪 __ 提交于 2020-01-14 06:18:26

问题


<block>
   <p>(...) nogen Forundring, med dyb Bedrøvelse, men <seg>end</seg> dybere Rolighed, læsde jeg Baggesens Svar til mig i Skilderiet No. 9 (...)</p>
</block>

I am all new when it comes to xslt. How do I pick the context, say three words before and after, the content tagged with the element ? I have been trying whit string-before and string-after but whit no success at all.

The result should look like this:

word: end

context: dyb Bedrøvelse, men end dybere Rolighed, læsde

回答1:


We do not know whether your XSLT processor supports XSLT 2.0, but since I wrote an answer before realizing that:

The reason the version is important is that the solution below uses a function that is available only in XSLT 2.0, namely tokenize(). Saying things like "selecting three words before and after" only make sense if you tokenize the strings you want to process in this fashion. Before tokenization, the concept of "words" is unknown to the XSLT processor.

Stylesheet

EDIT: As a response to your comment, I have slightly adapted the code to work with several seg elements. Of course, this introduces other difficulties that you need to 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>&#x0a;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>&#x0a;</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 and manipulate this solution online here.



来源:https://stackoverflow.com/questions/25377797/context-before-and-after-a-word-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!