how to escape single quote in xslt substring function
This should work:
<xsl:variable name="apos">'</xsl:variable>
...
<xsl:value-of select="substring-before(substring-after($datafromxml, concat('DataFromXML:', $apos)), $apos)" />
You could try swapping and " and ' in your xsl:value-of
<xsl:value-of select='substring-before
(substring-after($datafromxml,"DataFromXML:'"), "'")'/>
Alternatively, you could make use of the translate function to remove the pesky apostrophes
<xsl:value-of select='translate
(substring-after($datafromxml,"DataFromXML:"), "'", "")'/>
Not necessarily nicer, but it does remove the need for a variable.
The general rules for escaping are:
In 1.0:
- if you want the attribute delimiter in a string literal, use the XML
escape form
"
or'
- if you want the string delimiter in a string literal, you're hosed
In 2.0:
- if you want the attribute delimiter in a string literal, use the XML
escape form
"
or'
- if you want the string delimiter in a string literal, double it (for example, 'I can''t')
The use of a variable $quot or $apos as shown by Vitaliy can make the code much clearer.
Even in the most complicated case -- the string contains both a quote and an apostrophe -- the number can be extracted without resorting to variables.
Suppose we have this XML document:
<t>' "12345' "</t>
and want to extruct just the number.
In the following transformation we use a single XPath expression to do exactly that:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="text()">
<xsl:value-of select=
'substring-after(.,"'")
'/>
==============
<xsl:value-of select=
'substring-before(
substring-after(substring-after(.,"'"), '"'),
"'"
)
'/>
</xsl:template>
</xsl:stylesheet>
The result is:
"12345' "
==============
12345
Do note: I am intentionally having two XPath expressions -- the purpose of the first one is to make it simpler to understand what is expressed in the second XPath expression. Leaving just the last xsl:value-of
in the template body produces exactly the wanted result:
12345