xsl-list
[Top] [All Lists]

Re: for i = 1 to 10, or while (is there an equivalent?)

2004-11-09 23:48:11
Hi Steven,
  Please try this XSL -

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="text" />
        
<xsl:template match="/">
    <xsl:call-template name="escapeString">
       <xsl:with-param name="str" select="elem" />
       <xsl:with-param name="escapeChars" select="'\
&quot;'" />
       <xsl:with-param name="newstr" select="''" />
    </xsl:call-template>
</xsl:template>
        
<xsl:template name="escapeString">
    <xsl:param name="str" />
    <xsl:param name="escapeChars" />
    <xsl:param name="newstr" />
           
    <xsl:choose>        
        <xsl:when test="substring($str, 1, 1) != ''">
           <xsl:choose>                                      <xsl:when
test="contains($escapeChars, substring($str,1,1))">
                <xsl:call-template name="escapeString">
                  <xsl:with-param name="str"
select="substring($str, 2)" />
                  <xsl:with-param name="escapeChars"
select="$escapeChars" />
                  <xsl:with-param name="newstr"
select="concat($newstr, '\', substring($str, 1, 1))"
/>
                </xsl:call-template>
             </xsl:when>
             <xsl:otherwise>
                <xsl:call-template name="escapeString">
                  <xsl:with-param name="str"
select="substring($str, 2)" />
                  <xsl:with-param name="escapeChars"
select="$escapeChars" />
                  <xsl:with-param name="newstr"
select="concat($newstr, substring($str, 1, 1))" />
                </xsl:call-template>
             </xsl:otherwise>
          </xsl:choose>              
        </xsl:when>
        <xsl:otherwise>
           <xsl:value-of select="$newstr" />
        </xsl:otherwise>
      </xsl:choose>          
</xsl:template>
        
</xsl:stylesheet>

When it is applied to the XML -
<?xml version="1.0" encoding="UTF-8"?>
<elem>foo\bar"xxx</elem>

it produces o/p -
foo\\bar\"xxx

Regards,
Mukul

--- Steven Reddie <smr(_at_)essemer(_dot_)com(_dot_)au> wrote:

Hi,
 
I'm probably going about this the wrong way, but I
have a C string in a
variable that I want to escape the backslashes and
double-quotes prior to
output.  ie. foo\bar"xxx -> foo\\bar\"xxx
I figured I could use the XPath contain and
substring functions to do what I
want, but I can't find a way to iterate until there
are no more characters
of interest.  Can this be done in XSL/XPath?
 
Regards,
 
Steven



                
__________________________________ 
Do you Yahoo!? 
Check out the new Yahoo! Front Page. 
www.yahoo.com