Search code examples
xmlxslthtml-escape-characters

How to remove "<" from value in xml


In my xml there are such xml tags:

<field>&lt;</field>

I need to replace the "<" character with blank. I tried this statement but it prompts an error.

<xsl:value-of select="replace(.//field, '\&lt;', '')"/>

Error. Unable to generate the XML document using the provided XML/XSL input. Errors were reported during stylesheet compilation

How can I do it?


Solution

  • It is not clear whether you use an XSLT 1 processor where the XPath 2 function replace is simply not supported or whether the error is from the attempt to use the backslash or the attempt to call the replace function on several field element, in any case it should suffice to use

    <xsl:template match="field">
      <xsl:value-of select="translate(., '&lt;', '')"/>
    </xsl:template>
    

    in any XSLT version or

    <xsl:template match="field">
      <xsl:value-of select="replace(., '&lt;', '')"/>
    </xsl:template>
    

    in XSLT 2 or later.