xml - In xsl tokenize, how to get the preceding text value -


please suggest, how preceding text within tokenize. in sample, based preceded text web attribute applied. example if 'sky1996' type text preceded string 'moon', attribute value 'moon', otherwise 'sun'.

xml:

<article>  <text1>the solar eclipse sun: sky1996 happenned in 1996</text1>  <text2>the moon eclipse moon: sky1997 happenned in 1997</text2> </article> 

xslt 2.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:template match="@*|node()">         <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>     </xsl:template>      <xsl:template match="text1/text()|text2/text()">         <xsl:for-each select="tokenize(., ' ')">             <xsl:variable name="varprecededtext"><xsl:value-of select=".[preceding-sibling::node()[1]]"/></xsl:variable><!--within tokenize, preceded text value --><!--requesting suggestion -->             <xsl:choose>                 <xsl:when test="matches(., '^([s][k][y])\d{4}$')">                     <xsl:element name="web">                         <xsl:attribute name="href">                             <xsl:choose>                                 <xsl:when test="contains($varprecededtext, 'moon')">                                     <xsl:text>mooneclipse:</xsl:text>                                 </xsl:when>                                 <xsl:otherwise><xsl:text>suneclipse:</xsl:text></xsl:otherwise>                             </xsl:choose>                             <xsl:value-of select="."/>                         </xsl:attribute>                         <xsl:value-of select="."/>                     </xsl:element><xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>                 </xsl:when>                 <xsl:otherwise>                     <xsl:value-of select="."/>                     <xsl:if test="not(position()=last())"><xsl:text> </xsl:text></xsl:if>                 </xsl:otherwise>             </xsl:choose>         </xsl:for-each>     </xsl:template> 

required result:

<article> <text1>the solar eclipse sun: <web href="suneclipse:sky1996">sky1996</web> happenned in 1996</text1> <text2>the moon eclipse moon: <web href="mooneclipse:sky1997">sky1997</web> happenned in 1997</text2> </article> 

i use analyze-string:

<xsl:template match="text1/text()|text2/text()">     <xsl:analyze-string select="." regex="((\w+):)\s+([s][k][y]\d{{4}})">         <xsl:matching-substring>             <web href="{regex-group(2)}eclipse:{regex-group(3)}">                 <xsl:value-of select="regex-group(3)"/>             </web>         </xsl:matching-substring>         <xsl:non-matching-substring>             <xsl:value-of select="."/>         </xsl:non-matching-substring>     </xsl:analyze-string> </xsl:template> 

((\w+):)\s+([s][k][y]\d{{4}}) captures sun: sky1996 , moon: sky1997, sun , moon in group 2, sky1996 , sky1997 in group 3. these matches handled in xsl:matching-substring replace them desired structure. non-matches kept as-is.


Comments