1

I have this template to replace once string with another:

<xsl:template name="X">
<xsl:param name="field"/>
<xsl:param name="target"/>
<xsl:param name="change"/>
<xsl:value-of select='replace($field, $target, $change )' />  
</xsl:template> 

This is how I am calling it:

<xsl:call-template name="X">
<xsl:with-param name="field" select="artist"/>
<xsl:with-param name="target" select="'\\n'"/>
<xsl:with-param name="change" select="'&#10;'"/>
</xsl:call-template>

This works in most cases except for the case demonstrated here.
I am trying to replace the string "\n" with a HTML line break <br> or <br />.
I want an actual new line not the tag visible in the output. I know that it is matching on the \n but I can not make the line break happen. I either get a literal <br> in the output or nothing happens.

1 Answer 1

1

You will need to change your approach. Your template is using replace() which works with strings. You want to replace with markup. I'd use xsl:analyze-string instead:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/">
      <xsl:variable name="break"><br/></xsl:variable>
            
      <xsl:call-template name="X">
        <xsl:with-param name="field" select="artist"/>
        <xsl:with-param name="target" select="'\n'"/>
        <xsl:with-param name="change" select="$break"/>
      </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="X">
      <xsl:param name="field"/>
      <xsl:param name="target"/>
      <xsl:param name="change"/>
      <!-- <xsl:value-of select='replace($field, $target, $change )' />-->
      <xsl:analyze-string select="$field" regex="{$target}">
        <xsl:matching-substring>
          <xsl:sequence select="$change"/>
        </xsl:matching-substring>
        <xsl:non-matching-substring>
          <xsl:value-of select="."/>
        </xsl:non-matching-substring>
      </xsl:analyze-string>
    </xsl:template> 
    
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

4 Comments

Fantastic! it works for strings and <br>! Can you tell me what the curly braces do in this case? {$target}
the curly braces are an Attribute Value Template w3.org/TR/2021/REC-xslt20-20210330/#attribute-value-templates. The @regex was expecting a literal string, so in order to resolve the value of $target needed the AVT, otherwise it would use $target as the regex.
I'm also trying to replace an actual new line in the input with <br /> I tried matching on &#xa; and &#xd; but neither seem to match.
Post a simplified example of your input XML and XSLT in a qustion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.