I am looking for this solution in xslt 2.0:
Input: +47(12)1234567
Output: +47121234567
I tried to use this:
replace(replace('$Input', '(',''), ')','')
But it throws an error as '(' and ')' are unable to be escaped with the above code.
Please can some one point out the correct solution.
Thank you.
Parentheses are reserved characters in regex, which is what the replace function takes as argument.
The easiest solution is to use translate(), in this case:
<xsl:value-of select="translate('+47(12)1234567', '()', '')"/>
Alternatively, you can escape the parentheses with a backslash.
I would put both characters into square brackets in a single replace call replace($input, '[()]', '').
Use this: <xsl:value-of select="replace($input, '\(|\)','')"/>
Related
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 have tried to look at previous answers, but it is not clear to me. I would like to use the replace function to replace various characters that are causing me issues during saxon tranformation.
My code fragment is:
rdfs:comment "<xsl:value-of select="replace(DDTEXT, '["]|[']|[\\]|[\/]|[<]|[>]', '')"/>" ;
Saxon gives me the error:
XPST0003: XPath syntax error at char 23 on line 66 in {replace(DDTEXT, '["]|[']|}:expected ")", found "]"
I have spent several hours on this already... Any help appreciated. I am using xslt2, and have tried my regex using rubular, but I guess that did not help.
Try this one:
<xsl:value-of select="replace(., '["]|[']|[\\]|[/]|[<]|[>]', '')"/>
<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