<xsl:value-of select="replace('$#test', '$#test', '111111111111111')" />
how to make this work?
If I try to not using '$' sign everything works
<xsl:value-of select="replace('$#test', '#test', '111111111111111')" />
Try <xsl:value-of select="replace('$#test', '\$#test', '111111111111111')" />, assuming you want to treat the dollar sign literally. As it is a meta character in the regular expression language used to match the end of a string or a line to treat it literally you need to escape it.
For quick reference you may refer http://www.xml.com/pub/a/2003/06/04/tr.html
Related
I'm trying to replace a Register trademark symbol with an empty string.
I've tried:
<xsl:value-of select="replace( $myVariable, 'Ⓡ' ,'')"/>
But that didn't replace the Registered trademark symbol. Is there a way to replace it?
Your client is right and you are wrong. They are using the REGISTERED SIGN character (®). Your character (Ⓡ) is actually CIRCLED LATIN CAPITAL LETTER R.
If you are not going to use this symbol anymore from your variable, try to use the translate() function:
<xsl:value-of select="translate($myVariable, 'Ⓡ', '')"/>
Turned out that I was using the xsl:replace() function correctly. The client was using the symbol with the following unicode 174.
I have to to change a bad char to a quotation mark but I can't escape this last one.
Doing this doesn't work
<xsl:value-of select="fn:replace(prog:intitules/prog:intitule_fr,'¿', '\'')"/>
it produces
net.sf.saxon.trans.XPathException: Unmatched quote in expression
Same error with double or triple escapes '\' or '\\'.
My editor refuses this alternative syntax:
<xsl:value-of select='fn:replace(prog:intitules/prog:intitule_fr,"¿", "'")'/>
Any idea ?
Bernard
Try it this way:
<xsl:value-of select='replace(input, "¿", "'")'/>
In XPath 2.0+, you can escape an apostrophe within an apostrophe-delimited string literal by doubling it. So try:
''''
You need to think very carefully about escapes when you're using regular expressions within XPath within XSLT. Why does the character need escaping?
If it has a special meaning in regular expressions (for example '(') then use a backslash
If it isn't allowed because of XPath rules (like here), use XPath escaping (write 'O'Neil' as 'O''Neil' or "a="3"" as "a=""3""")
If it isn't allowed because of XML rules (e.g. "<"), use XML escaping (write < as <)
The reason this doesn't work:
<xsl:value-of select='fn:replace(prog:intitules/prog:intitule_fr,"¿", "'")'/>
is that the XML parser is treating the apostrophe within the string literal as marking the end of the value of the select attribute. So here you have an XML issue, and under rule 3 you therefore need to use XML escaping (')
Assuming your files are utf-8 encoded, you could you try a workaround, using the Unicode apostrophe character (hexadecimal 2BC) instead of quote (hexadecimal 27):
<xsl:value-of select="fn:replace(prog:intitules/prog:intitule_fr,'¿', 'ʼ')"/>
Edited: searching a little bit more, I discovered that switching ' and " and using the entity ' will get the same result, as michael.hor257k proposed meanwhile:
<xsl:value-of select='fn:replace(prog:intitules/prog:intitule_fr,"¿", "'")'/>
I have a solution where the filename has a prefix showing the filesize of a PDF. I need to pick up that value in to a XML-file that has a lot of other info that is collected with the XSLT.
How ever I can't get just this Regex match to work.
Filename have this structure as this example:
776524_P9466_Novilon_Broschyr_SE_Omslag.xml where the digits before the underscore is the filesize.
I have a Regex search pattern of _(.*) and I can validate that it will match everything after the first section of the digits.
Here is my XSL that I'm having problems with:
<xsl:param name="find_size">
<xsl:text>(_.*)</xsl:text>
</xsl:param>
<xsl:variable name="filename_of_start"><xsl:value-of select="replace($filename_of_file, '$find_size', '')"/></xsl:variable>
<artwork_size><xsl:value-of select="$filename_of_start"/></artwork_size>
$filename_of_file has the string: 776524_P9466_Novilon_Broschyr_SE_Omslag.xml
I have also tried to match the digits before the underscore and replace with that match but haven't got that to work either. Other replaces where I remove other matches from the beginning of the string works.
Thanks
How about using the substring-before() XPath function?
<xsl:variable name="file_size" select="substring-before($filename, '_')" />
Instead of replace($filename_of_file, '$find_size', '') you want replace($filename_of_file, $find_size, '').
I am new to XSLT. I googled extensively but couldn't figure out how to do the following:
I am transforming XML to LaTeX. Of course, LaTeX needs to escape characters such as $ and #. I tried the following in the replace function but it does not work. (They do work without the replace function.)
<xsl:template match="xyz:doc">
\subsubsection{<xsl:value-of select="replace( xyz:headline, '(\$)', '\$1' )"/>}
...
</xsl:template>
<xsl:template match="xyz:doc">
\subsubsection{<xsl:value-of select="replace( xyz:headline, '\$', '\$' )"/>}
...
</xsl:template>
Possible content to be escaped is:
"Locally defined field #931" or
"Locally defined subfield $b"
What am I doing wrong?
Many thanks for your answers!
If you want to replace a dollar symbol $ in the input with \$ in the output then use replace(xyz:headline, '\$', '\\\$').
If there are several characters that need the same escaping then replace(xyz:headline, '([$#])', '\\$1') should do.
Sample at http://xsltransform.net/bdxtqX/1
I try to translate(tag, ''', ''), but it doesn't work. Is it possible to delete or change symbol ' ?
Best regards.
I find it best to use variables for this:
<xsl:variable name="apos">'</xsl:variable>
<xsl:variable name="quot">"</xsl:variable>
<xsl:value-of select="translate(., $apos, $quot)"/>
Either replace the innermost ' with ' or use quotation marks to delimit a string containing an apostrophe.
I want to offer another answer, because the others didn't work when I use this method.
I have solved it in a different way.
You should do as this:
<xsl:value-of select='translate(translate(translate(normalize-space(#onclick),"()",""),"'",""),"submitLSthis, product.php?p=","")' />
It should be
select='', not select="",
and
translate(translate(translate(normalize-space(#onclick),"()",""),"'",""),"submitLSthis, product.php?p=","")
not
translate(translate(translate(normalize-space(#onclick),'()',''),''',''),'submitLSthis, product.php?p=','')
I hope this is helpful.