This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: XSL is NOT a string processing language!


Philippe Figon <philippe dot figon at passager dot org> wrote: 

> Hello,
> 
> First I'd like to say that I _know_ XSL is not a string processing 
> language, but I am facing a problem I wish I could solve inside XSL 
> itself.
> 
> Here it is : writing style sheets to convert XML in LaTeX I have to 
> makesome characters substitutions due to the "escape chars".
> I'm heavily using David Carlisle string-replace template, it works 
> fine but because of its "recursiveness" I can't obtain sequential 
> replacement :
> 
> If I want to replace 'a' by 'bX' and 'b' by 'aY' (which really 
> happens with \ and { for example), within the string 'bias' I will 
> _never_ obtain 'aYibXs' which is what I want, but aYiaYXs or bXYibXs 
> dependind on the order of the calls to the string-replace template.
> 
> Due to the lack of "real" variables I can't find a way of doing this,

> I've tried out many different ways but it's always endind with a "oh 
> no, you can't change the value of a variable and pass it to another 
> template". One solution could have been the use of global variables, 
> but if you change them, the change will be available only in the 
> template where you made it (kind of local global variable ;-)), no 
> way to escape this circle.
> 
> Does anyone think it's possible to do such sequential substitution ?

There's absolutely no problem. Using FXSL and the str-map() function,
one will write:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:testmap="testmap"
exclude-result-prefixes="testmap"
>
   <xsl:import href="str-map.xsl"/> 

   <testmap:testmap/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>
   
   <xsl:template match="/">
     <xsl:variable name="vTestMap" 
          select="document('')/*/testmap:*[1]"/>
     <xsl:call-template name="str-map">
       <xsl:with-param name="pFun" select="$vTestMap"/>
       <xsl:with-param name="pStr" select="'bias'"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template name="rep" match="testmap:*">
      <xsl:param name="arg1"/>
      
      <xsl:choose>
        <xsl:when test="$arg1 = 'a'">
          <xsl:value-of select="'bX'"/>
        </xsl:when>
        <xsl:when test="$arg1 = 'b'">
          <xsl:value-of select="'aY'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="$arg1"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

This, when applied to any xml source produces exactly the desired
result:
aYibXs


Cheers,
Dimitre Novatchev.


__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]