Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to escape single quote in xslt substring function

Tags:

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:&#39;')"/>  <xsl:value-of select="substring-after('$datafromxml','DataFromXML:&apos;')"/>  <xsl:value-of select="substring-after('$datafromxml','DataFromXML:'')"/> 

Error:

String literal was not closed 'DataFromXML:'--->'<--- 
like image 772
buttowski Avatar asked Sep 13 '12 10:09

buttowski


People also ask

What is substring after in XSLT?

substring-after() Function — Returns the substring of the first argument after the first occurrence of the second argument in the first argument. If the second argument does not occur in the first argument, the substring-after() function returns an empty string.

How do I replace in XSLT?

XSLT replace is deterministic and does string manipulation that replaces a sequence of characters defined inside a string that matches an expression. In simple terms, it does string substitution in the specified place by replacing any substrings. Fn: replace function is not available in XSLT1.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


2 Answers

The general rules for escaping are:

In 1.0:

  • if you want the attribute delimiter in a string literal, use the XML escape form &quot; or &apos;
  • 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 &quot; or &apos;
  • 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.

like image 200
Michael Kay Avatar answered Oct 01 '22 23:10

Michael Kay


This should work:

<xsl:variable name="apos">'</xsl:variable>  ...  <xsl:value-of select="substring-before(substring-after($datafromxml, concat('DataFromXML:', $apos)), $apos)" /> 
like image 37
Vitaliy Avatar answered Oct 01 '22 23:10

Vitaliy