[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Placing mark-up in between strings


Subject: Re: [xsl] Placing mark-up in between strings
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Thu, 23 Nov 2006 12:47:31 +0100 (CET)

Jeff Sese wrotet:

  Hi

> I'm not sure but i think it has something to do with the
> ati:replace-with-nodes-1 function call in
> ati:replace-with-nodes.  this should return a xs:string
> and not a node() as per requirement in the 1st parameter
> of ati:replace-with-nodes function.

  Yes, you're right, it is completely wrong.  Still too
early in the morning I guess...  Actually, I wanted to avoid
dealing with regex as there is no (if I'm right) an escaping
function.  I mean a function that escapes a string to can be
included in a regex to match exactly, even if it contains
regex-special characters.

  But if your keywords don't contain such special characters
and if there is no keyword that can be sub-match of another,
you can use the following:

    drkm[30] ~/xslt/tests$ cat replace-with-nodes.xsl
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    xmlns:ati="ati-ns"
                    exclude-result-prefixes="xs ati"
                    version="2.0">

      <xsl:output omit-xml-declaration="yes" indent="yes"/>

      <!--
          Replace strings with an element constructed from the replaced
          and replacement strings.  Build those elements then call
          ati:replace-with-nodes().
      -->
      <xsl:function name="ati:replace-specific" as="node()+">
        <xsl:param name="input"            as="xs:string"/>
        <xsl:param name="words-to-replace" as="xs:string*"/>
        <xsl:param name="replacement"      as="xs:string*"/>
        <xsl:variable name="new-replacement" as="element()*">
          <xsl:for-each select="1 to count($words-to-replace)">
            <xsl:variable name="i" select="."/>
            <replacement value="{ $replacement[$i] }">
              <xsl:value-of select="$words-to-replace[$i]"/>
            </replacement>
          </xsl:for-each>
        </xsl:variable>
        <xsl:sequence select="
            ati:replace-with-nodes(
                $input,
                $words-to-replace,
                $new-replacement
              )"/>
      </xsl:function>

      <!--
          Replace strings with given nodes.  Strings are concatenated
          then used as a regex.  Be carefull with special-regexp
          characters.
      -->
      <xsl:function name="ati:replace-with-nodes" as="node()+">
        <xsl:param name="input"            as="xs:string"/>
        <xsl:param name="words-to-replace" as="xs:string*"/>
        <xsl:param name="replacement"      as="node()*"/>
        <xsl:variable name="regex" select="
            string-join(
                for $w in $words-to-replace return
                  concat('(', $w, ')'),
                '|'
              )"/>
        <xsl:analyze-string select="$input" regex="{ $regex }">
          <xsl:matching-substring>
        <xsl:variable name="i" as="xs:integer" select="
            (1 to count($words-to-replace))[regex-group(.)]"/>
            <xsl:sequence select="$replacement[$i]"/>
          </xsl:matching-substring>
          <xsl:non-matching-substring>
            <xsl:value-of select="."/>
          </xsl:non-matching-substring>
        </xsl:analyze-string>
      </xsl:function>

      <!--
          Simple test.
      -->
      <xsl:template name="main">
        <xsl:variable name="input" as="element()">
          <p>This is supposed to be my input string with search1,
            search2, and search3.</p>
        </xsl:variable>
        <xsl:variable name="words" select="
            'search1', 'search2', 'search3'"/>
        <xsl:variable name="replacement" select="
            'replacement1', 'replacement2', 'replacement3'"/>
        <initial>
          <xsl:sequence select="$input/node()"/>
        </initial>
        <replaced>
          <xsl:sequence select="
              ati:replace-specific(
                  string($input),
                  $words,
                  $replacement
                )"/>
        </replaced>
      </xsl:template>

    </xsl:stylesheet>

    drkm[31] ~/xslt/tests$ saxon -it main replace-with-nodes.xsl
    <initial>This is supposed to be my input string with search1,
            search2, and search3.</initial>
    <replaced>This is supposed to be my input string with <replacement
value="replacement1">search</replacement>1,
            <replacement value="replacement1">search</replacement>2,
and <replacement value="replacement1">search</replacement>3.</replaced>

  Regards,

--drkm






















	

	
		
___________________________________________________________________________ 
Dicouvrez une nouvelle fagon d'obtenir des riponses ` toutes vos questions ! 
Profitez des connaissances, des opinions et des expiriences des internautes sur Yahoo! Questions/Riponses 
http://fr.answers.yahoo.com


Current Thread