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.
Related
I am trying to either eliminate or replace the acute accent character with a simple single quote character in xslt 1.0.This is what I have tried so far and none of them replace that character. What could be wrong with my translate construct?
<xsl:value-of select="translate(HeaderFields/Name,'#xb4','')"/>
<xsl:value-of select="translate(HeaderFields/Name,'0xb4','')"/>
<xsl:value-of select="translate(HeaderFields/Name,'´','\'')"/>
Try:
<xsl:value-of select='translate(HeaderFields/Name,"´","'")'/>
Note the exchange of double and single quotation marks - see explanation at: https://www.w3.org/TR/1999/REC-xpath-19991116/#section-Introduction
You can express an acute accent as '´' or as ´. Neither '#xb4' nor '0xb4' is correct. (Were you just guessing?)
Expressing the single quote is a bit trickier. In XSLT 2.0 you can write it as '''', but that doesn't work in 1.0. In 1.0 the best way is to use a variable:
<xsl:variable name="apos">'</xsl:variable>
<xsl:value-of select="translate(HeaderFields/Name,'´',$apos)"/>
Finally, you need to be aware that an acute accent might not be a free-standing Unicode character; it might be combined with the character that it modifies, so for example á could be expressed as the single code point xE1. To ensure that it is a separate character you need to convert the text to decomposed normal form, which can be done in 2.0 using the normalize-unicode() function, but this is not available in 1.0.
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 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
<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
I try to use upper-case() in an XPATH, my parser is MSXML 4.0, and I get :
upper-case is not a valid XSLT or XPath function.
Is it really not implemented ?
There are no functions in xslt 1.0 to convert to uppercase or lowercase. Instead do the following:
If it is required in a lot of places:
Declare these two xsl variables (this is to make the xslt more readable)
<!-- xsl variables up and lo and translate() are used to change case -->
<xsl:variable name="up" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:variable name="lo" select="'abcdefghijklmnopqrstuvwxyz'"/>
And use them in your translate function to change the case
<xsl:value-of select="translate(#name,$lo,$up)"/>
If you need to use it in just one place, no need to declare variables
<xsl:value-of select="translate(#name,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
Maybe this can help you:
translate(string, string, string)
The translate function takes a string and, character-by-character, translates characters which match the second string into the corresponding characters in the third string. This is the only way to convert from lower to upper case in XPath. That would look like this (with extra white space added for readability). This code would translate the employee last names to upper case and then select those employees whose last names begin with A.
descendant::employee[
starts-with(
translate(#last-name,
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
"A"
)
]
If the second string has more characters than the third string, these extra characters will be removed from the first string. If the third string has more characters than the second string, the extra characters are ignored.
(from http://tutorials.beginners.co.uk/professional-visual-basic-6-xml-part-1-using-xml-queries-and-transformations.htm?p=3)