I am trying to filter out problem characters (quotes and slashes) while doing an XSLT translation but am unable to actually remove them. I've tried several proposed solutions here and they have been unsuccessful:
Replace special characters in XSLT
Removing double quotes in XSL
XSL: replace single and double quotes with ' and "
I would ideally like to replace the characters with some kind of marked word, like quotes or slash, but at this point I'd be fine just stripping them out for now.
I'm only running it on a couple selects, so it shouldn't be that hard. I'm not sure what is going wrong.
<xsl:value-of select="ns3:stepTitle"/>
EDIT:
Need to use XML 1.0.
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/text()">
<xsl:value-of select="translate(., '\"', '*quote*')"/>
</xsl:template>
</xsl:stylesheet>
XML:
<test>
I need to remove "quotes" and slashes /\ from here.
</test>
The result was:
<?xml version="1.0" encoding="UTF-16"?>
<test>
I need to remove qquotesq and slashes /* from here.
</test>
It's not 100% clear what your problem is, but I'm guessing it is a variant of the problem described in this old thread from 2001. If so, the following is an example XSLT 1.0 stylesheet to replace ASCII apostrophe characters into U+2019 RIGHT SINGLE QUOTATION MARK (Unicode code point 8217 in decimal) characters. The "trick" is to define a variable holding a single-character string containing the apostrophe character, and then use the variable in calls to translate()
(but could also be used with concat()
to create strings with apostrophe characters):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text()">
<xsl:variable name="apos" select='"'"'/>
<xsl:variable name="string-containing-quotes" select="."/>
<xsl:variable name="string-with-quotes-replaced"
select="translate($string-containing-quotes, $apos, '’')"/>
<xsl:value-of select="$string-with-quotes-replaced"/>
</xsl:template>
</xsl:stylesheet>
You can test the stylesheet with a test XML input document such as
<test>
Text containing 'apostrophe' characters
</test>
Maybe not widely enough known feature of translate
function is that the replace
string (3-rd argument) can be shorter than the from string (2nd argument).
In such a case, characters from the source string (1st argument) which:
- occur in the from string,
- but have no corresponding characters in the replace string
are deleted.
So you have to use translate(., '/"', '')
.
The from string has 2 chars: a slash (/
) and a double quote "
and the replace string is empty, so both these chars will be deleted.
The example script is below:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*/text()">
<xsl:value-of select="translate(., '/"', '')"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
Note: In your example you put a backslash (not a forward slash).