How to parse text nodes in xml using XSLT 2.0 functions



Let's say I have this temporary document:



<xsl:variable name="var">
<root>
<sentence>Hello world</sentence>
<sentence>Foo foo</sentence>
</root>
</xsl:variable>


I am using analyze-string to find "world" string and wrap it with element called <match>



<xsl:function name="my:parse">
<xsl:param name="input"/>
<xsl:analyze-string select="$input" regex="world">
<xsl:matching-substring>
<match><xsl:copy-of select="."/></match>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:copy-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>


This function will return:



Hello <match>world</match>Foo foo


Naturally I want this output:



<root>
<sentence>Hello <match>world</match></sentence>
<sentence>Foo foo</sentence>
</root>


Nevertheless, I know why is my function doing this, but I just can't figure it out how to copy elements and inject them with new content. I know there is issue with context item but I tried so many other ways and nothing works for me.


On the other hand, using template that match all text works fine. But requirement is to use function (because I am working on multiphase transform and I want to use functions that represent each step). I wonder is there solution to this problem? Am I missing something fundamental?


No comments:

Post a Comment