Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Translating characters and enties (http://www.velocityreviews.com/forums/t166352-translating-characters-and-enties.html)

Don Robertson 01-05-2004 11:47 AM

Translating characters and enties
 
Greetings,

I need to be able to output text with some characters escaped - eg change a

 to \n

I am at a loss as how to do this. I am trying

translate($text,'
','\n')

and

<!DOCTYPE stylesheet [
<!ENTITY linebreak
"\n">
]>

translate($text,'&#xA;','&linebreak;')

but all I am getting is the '\'

I have just realised - would I have to do a recursive contains
substring-before substring-after loop? it is to late to try now ... but if
anyone has a suggestion on how to do this Id appreciate it

Don

Patrick TJ McPhee 01-05-2004 04:00 PM

Re: Translating characters and enties
 
In article <pan.2004.01.05.11.47.00.283140@robertson.net.nz >,
Don Robertson <don@robertson.net.nz> wrote:

% I am at a loss as how to do this. I am trying
%
% translate($text,'&#xA;','\n')

translate() works on single characters. This will change each new-line
to a back-slash, and do nothing with the n.

You want a `replace' function, but there's none provided in xpath.
When faced with this situation, it's often helpful to visit
http://www.exslt.org and see if there's something there which does
what you want (many XSLT processors support the exslt extensions).

In this case, there's str:replace, so you could have
<xsl:template match='text()' xmlns:str='http://exslt.org/strings'>
<xsl:value-of select='str:replace(., '&xA;', '\n')
</xsl:template>

If that doesn't work, it's possible to create a template which performs
replace operations. I've appended mine below. You use it like this:

<!-- xsl:import must go at the top of the stylesheet -->
<xsl:import href="replacesubstring.xsl"/>

<xsl:template match='text'()'>
<xsl:call-template name='replace-substring'>
<xsl:with-param name="original">
<xsl:value-of select="string(.)"/>
</xsl:with-param>
<xsl:with-param name="substring">
<xsl:text>&#xA;</xsl:text>
</xsl:with-param>
<xsl:with-param name="replacement">
<xsl:text>\n</xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:template>

<?xml version="1.0"?>
<!-- replace function by P McPhee -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template name="replace-substring">
<xsl:param name="prefix"/>
<xsl:param name="original"/>
<xsl:param name="substring"/>
<xsl:param name="replacement" select="''"/>

<xsl:choose>
<xsl:when test="contains($original, $substring)">
<xsl:call-template name="replace-substring">

<xsl:with-param name="prefix">
<xsl:value-of select="concat($prefix, substring-before($original, $substring), $replacement)"/>
</xsl:with-param>

<xsl:with-param name="original">
<xsl:value-of select="substring-after($original, $substring)"/>
</xsl:with-param>

<xsl:with-param name="substring">
<xsl:value-of select="$substring"/>
</xsl:with-param>

<xsl:with-param name="replacement">
<xsl:value-of select="$replacement"/>
</xsl:with-param>

</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($prefix, $original)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com

Dimitre Novatchev 01-05-2004 06:52 PM

Re: Translating characters and enties
 

"Don Robertson" <don@robertson.net.nz> wrote in message
news:pan.2004.01.05.11.47.00.283140@robertson.net. nz...
> Greetings,
>
> I need to be able to output text with some characters escaped - eg change

a
> &#xA; to \n
>
> I am at a loss as how to do this. I am trying
>
> translate($text,'&#xA;','\n')
>
> and
>
> <!DOCTYPE stylesheet [
> <!ENTITY linebreak
> "\n">
> ]>
>
> translate($text,'&#xA;','&linebreak;')
>
> but all I am getting is the '\'
>
> I have just realised - would I have to do a recursive contains
> substring-before substring-after loop? it is to late to try now ... but if
> anyone has a suggestion on how to do this Id appreciate it



Using FXSL this is very straightforward.

This transformation:

<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"/>
<xsl:output method="text"/>

<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="/*"/>
</xsl:call-template>
</xsl:template>

<testmap:testmap/>
<xsl:template name="putNL" match="*[namespace-uri() = 'testmap']">
<xsl:param name="arg1"/>

<xsl:choose>
<xsl:when test="$arg1 = '&#xA;'">\n</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$arg1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

when applied on this source.xml:

<t>
This is a multiline text -- line1
Line2
Line3
</t>

Produces the wanted result:

\nThis is a multiline text -- line1\nLine2\nLine3\n


Hope this helped.


Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html





All times are GMT. The time now is 07:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.