xslt - removing quotes, single quotes, <, >, \, / using the replace function - replace

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, '["]|[&apos;]|[\\]|[\/]|[<]|[>]', '')"/>" ;
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(., '[&quot;]|[&apos;]|[\\]|[/]|[&lt;]|[&gt;]', '')"/>

Related

What escape character for fn:replace function

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, "¿", "&apos;")'/>
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 (&apos;)
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 &apos; will get the same result, as michael.hor257k proposed meanwhile:
<xsl:value-of select='fn:replace(prog:intitules/prog:intitule_fr,"¿", "&apos;")'/>

XSLT 2.0 - How to replace '(' and ')' to empty from the string?

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, '\(|\)','')"/>

XPathException: No expression before quantifier - but I can't find its cause

I am working on an XSLT transformation from XML to CSV. As the XML is huge I work on skipping nodes that match an entry in a blacklist, defined in the stylesheet.
I am using Saxon 9 HE as XSLT engine, called from within Eclipse.
I receive a stacktrace like this:
JAXPSAXProcessorInvoker - Syntax error at char 34 in regular expression: No expression before quantifier; SystemID: file:/C:/Users/Public/workspace/TraceToCSV.xsl; Line#: 97; Column#: 31
; SystemID: file:/C:/Users/Public/workspace/TraceToCSV.xsl; Line#: 97; Column#: 31
net.sf.saxon.trans.XPathException: Syntax error at char 34 in regular expression: No expression before quantifier
at net.sf.saxon.regex.ARegularExpression.<init>(ARegularExpression.java:57)
at net.sf.saxon.java.JavaPlatform.compileRegularExpression(JavaPlatform.java:326)
and the call in my stylesheet is:
...
<xsl:when test="$blacklist/entry[matches(text(), $textdata, 'i')]">
<xsl:message>
<xsl:text>'</xsl:text>
<xsl:value-of select="$textdata" />
<xsl:text>' is blacklisted</xsl:text>
</xsl:message>
</xsl:when>
where $blacklist is defined in an earlier location similar to
<xsl:variable name="blacklist">
<entry>^first expression$</entry>
<entry>^second expression$</entry>
<entry>^and so on$</entry>
<entry>^with some digits \d+$</entry>
</xsl:variable>
The error message is confusing because 'at char 34 in regular expression' makes no sense since they are all shorter.
The stylesheet filters out quite a few of the entries before it fails, so it must basically be right. My only though is that there is a problem with escaping \d+ differently, as this is the only regex having a quantifier, but I tried already with a double backslash and it did not help.
Any suggestions?
The error is in the matches call - input and pattern were in the wrong order.

xslt 2.0 how replace $ by escaped dollar (for conversion to LaTeX)

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

How to express a string containing both apostrophes and quotes in XPath?

I'll spare you the details because they would be needlessly confusing. Long story short, I'm using XSLT 1.0 to generate XSL documents, I'm trying to compare a variable to a literal string, and that string may contain quotes and apostrophes.
For the sake of simplicity, let's say that this literal is composed of two characters: a quote followed by an apostrophe. In reality, it can be any text really. Is there a simpler way to do this:
<xsl:if test="$var = concat('"', "'")">
than this?
<xsl:variable name="str">"'</xsl:variable>
<xsl:if test="$var = $str">
I have checked XPath's specs and there doesn't seem to be a way to escape characters, so the following would not work as desired:
<xsl:if test="$var = '"&apos;'">
Thanks!
There's no way to do it neatly in XPath 1.0. In XPath 2.0, you can escape both kinds of quotes by doubling.
& quot;& amp;&(!)apos; -looks much better, but what did you want to get?
In anyway: once I have written application that deals with producing of Javascript over XSLT.
The same problem with huge number of & quot;,... we solved in 2 ways:
Declare global xsl:param, $q - looks shorter than & quot;
Use 'translate' XPath function, make assumption '!' - is a & quot;, # is a & amp; ..