0

I am using a translate() function in a xslt script to remove unwanted string from a repeating element before rendering the output. While the translate function is doing its job, it is affecting a handful of these elements by removing more characters than it should and I can't for the life of me figure out why that is happening.

Here's an example XML document:

<?xml version="1.0" encoding="utf-8"?><books><book><title>Keep this text [remove this text]</title></book><book><title>Keep this text 2 [remove this text]</title></book><book><title>Keep this text 3 [remove this text]</title></book><book><title>Keep this text 4</title></book></books>

Here's a VERY basic example portion of a XSLT that tests for the [remove this text] string from the title element (this function is nested in a for-each loop):

<xsl:value-of select="translate(books/book/title, '[remove this text]', '')" />

This successfully removes the unwanted string and in most cases preserves the preceding text without issue. Unfortunately, in a handful of issues, the resultant output of the title element will be missing several characters (all white space is removed) and will look like "kpptt2" instead of "Keep this text 2". Does any one know why this might happen and/or produces flukes such as this example? Thank you!

wyattburp86
  • 51
  • 1
  • 8
  • 1
    You are confusing `translate()` with `replace()`. `translate()` replaces individual characters with other characters. `replace()` (which does not exist in XSLT 1.0) replaces a string with another string. – michael.hor257k Feb 02 '19 at 01:19
  • Understood that translate() is not the ideal solution, but is there a better way to treat this in XML 1.0? – wyattburp86 Feb 02 '19 at 01:25
  • 1
    Use a recursive template: https://stackoverflow.com/a/30339654/3016153 – michael.hor257k Feb 02 '19 at 01:45

3 Answers3

0
    <xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="*/text()">
    <xsl:choose>
        <xsl:when test="contains(., '[remove this text]')">
            <xsl:value-of select="substring-before(., ' [remove this text]')"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="."/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

version 1.0 use

Sam
  • 841
  • 1
  • 5
  • 15
-1
    <xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="//text()">
    <xsl:value-of select="replace(., '\s\[\w+\s\w+\s\w+\]', '')"/>
</xsl:template>

plz check

Sam
  • 841
  • 1
  • 5
  • 15
-1
    <xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="//text()">
    <xsl:value-of select="replace(., '\s\[remove\sthis\stext\]', '')"/>
</xsl:template>

This code particular text '[remove this text]' remove

Sam
  • 841
  • 1
  • 5
  • 15