I tried to substring data with single quote in XSLT:
String : DataFromXML:'12345'
expected Result: 12345
<xsl:value-of select="substring-after('$datafromxml','DataFromXML:')"/>
Result: '12345'
i tried below code
<xsl:value-of select="substring-after('$datafromxml','DataFromXML:'')"/>
<xsl:value-of select="substring-after('$datafromxml','DataFromXML:'')"/>
<xsl:value-of select="substring-after('$datafromxml','DataFromXML:'')"/>
Error:
String literal was not closed 'DataFromXML:'--->'<---
The general rules for escaping are:
In 1.0:
"
or'
In 2.0:
"
or'
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:
and want to extruct just the number.
In the following transformation we use a single XPath expression to do exactly that:
The result is:
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:This should work:
You could try swapping and " and ' in your xsl:value-of
Alternatively, you could make use of the translate function to remove the pesky apostrophes
Not necessarily nicer, but it does remove the need for a variable.