views:

28

answers:

2

Hi,

Given XML of:

<data>
<field>Value 1&#xD;Value 2&#xD;Value 3&#xD;</field>
</data>

I would like to be able to create some HTML to display the values and convert the &#xD; into <br/>

<html>
<head/>
<body>
<div>Field Values</div>
<div>Value 1<br/>Value 2<br/>Value 3<br/></div>
</body>
</html>

However I can't think of way to do this using XSL version 1.0?

I tried

<xsl:value-of select="field" disable-output-escaping="yes"/>

However the text all appears on 1 line.

Any ideas/suggestions?

Thanks

Dave

+1  A: 

Here is how you can replace &#xD; with <br/>:

<xsl:template match="data">
  <xsl:call-template name="add-br">
    <xsl:with-param name="text" select="field" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="add-br">
  <xsl:param name="text" select="."/>
  <xsl:choose>
    <xsl:when test="contains($text, '&#xD;')">
      <xsl:value-of select="substring-before($text, '&#xD;')"/>
      <br/>
      <xsl:call-template name="add-br">
        <xsl:with-param name="text" select="substring-after($text,'&#xD;')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
  <xsl:value-of select="$text"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
True Soft
Cool.. Thanks.. only 1 thing.. you need to add the With Param to the call template at the top.. other than that.. 100% working. whooooooooop
CraftyFella
It works without `with-param` also, because it is inside the node `data`.
True Soft
+2  A: 

You can use a recursive template do do the replacement:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <data>
        <xsl:call-template name="replaceCharsInString">
          <xsl:with-param name="stringIn" select="data/field" />
        </xsl:call-template>
      </data>
    </xsl:template>

  <xsl:template name="replaceCharsInString">
    <xsl:param name="stringIn"/>
    <xsl:choose>
      <xsl:when test="contains($stringIn,'&#xD;')">
        <xsl:value-of select="substring-before($stringIn,'&#xD;')"/>
        <br />
        <xsl:call-template name="replaceCharsInString">
          <xsl:with-param name="stringIn" 
                          select="substring-after($stringIn,'&#xD;')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$stringIn"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
0xA3
Brill... Thanks
CraftyFella