how to translate quote symbol? - xslt

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 &apos; 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),"()",""),"&apos;",""),"submitLSthis, product.php?p=","")' />
It should be
select='', not select="",
and
translate(translate(translate(normalize-space(#onclick),"()",""),"&apos;",""),"submitLSthis, product.php?p=","")
not
translate(translate(translate(normalize-space(#onclick),'()',''),'&apos;',''),'submitLSthis, product.php?p=','')
I hope this is helpful.

Related

I would like to include apostrophe in the output attributes

I wrote a code to eradicate all the special characters with a function.
<xsl:function name="lancet:stripSpecialChars">
<xsl:param name="string" />
<xsl:variable name="AllowedSymbols"
select="'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'"/>
<xsl:value-of select="
translate(
$string,
translate($string, $AllowedSymbols, ' '),
' ')
"/>
</xsl:function>
<xsd:element xtt:fixedLength="14" xtt:required="true" xtt:severity="error" xtt:align="left">
<xsl:value-of select="lancet:stripSpecialChars(upper-case(replace(normalize-unicode(translate($emp/wd:First_Name, ',', ' '), 'NFKD'), '⁄', '/')))"/>
</xsd:element>
Now there is a requirement for me to include apostrophe ('). When I am trying to include the same in AllowedSymbols, I am getting an error.
The output Right now is D AGOSTINO. I need something like D'AGOSTINO.
Not sure how to handle this. Could someone please help me out with this. Thanks
You don't say what the error is, but you probably just need to escape the apostrophe in your variable.
This is done by doubling up the apostrophe:
<xsl:variable name="AllowedSymbols" select="'''ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'"/>
Since you're using XSLT 2.0, you should be able to use replace() instead of translate()...
<xsl:function name="lancet:stripSpecialChars">
<xsl:param name="string"/>
<xsl:value-of select="replace($string,'[^A-Z0-9'']','')"/>
</xsl:function>
I'm not replacing lowercase letters since the string you're passing is already forced to uppercase, but if you use the function elsewhere you can add a-z to the character class.
Encode it as &apos; (’ also)
Enclose the value in a CDATA section (recommended as you get rid of encoding problems.
<data><![CDATA[some stuff including D'Agostino & other reserved/problematic characters :-) ]]></data>

replace string in xslt 2.0 with replace function

I have a string like this
"My string"
Now I want to replace my with best so that the output will be like best string.
I have tried some thing like this
<xsl:value-of select="replace( 'my string',my,best)"/>
but probably its a wrong syntax
I have googled a lot but found nothing..every where the mechanism to do this XSLT 1.0 is explained.Can any one tell me how to do it in XSLT 2.0 ,The easy way compared to 1.0
Given:
<xsl:variable name="s1" select="'My string'"/>
Simply use:
<xsl:value-of select="replace($s1, 'My', 'best')"/>
Note that a regular expression is applied. Meaning:
<xsl:value-of select="replace('test.replace', '.', ':')"/>
Becomes:
::::::::::::
Be sure to escape the characters that have special meaning to the regular expression interpreter:
<xsl:value-of select="replace('test.replace', '\.', '::')"/>
Becomes:
test::replace
First check, if your xslt processor (saxxon) is the latest release. Then you have to set
<xsl:stylesheet version="2.0" in the head of your xslt-stylesheet. That's it.
Your code was fine, besides you forgot the apostrophs:
<xsl:value-of select="replace( 'my string',my,best)"/>
must be
<xsl:value-of select="replace('my string','my','best')"/>

replace function doesn't work with '$' symbol

<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

XSL - Escaping an apostrophe during xsl:when test

I have the following code which appears to be failing.
<xsl:when test="$trialSiteName = 'Physician&apos;s Office'">
Also, visual studio is complaining saying
"Expected end of expression, found 's"
How am I supposed to escape the character?
XSLT v1.0. Apache XSL-FO processor.
Much more simple -- use:
<xsl:when test="$trialSiteName = "Physician&apos;s Office"">
Declare a variable:
<xsl:variable name="apos" select='"&apos;"'/>
Use the variable like this in the <xsl:when> clause:
<xsl:when test="$trialSiteName = concat('Physician', $apos, 's Office')">
&apos; works for XPath 1.0. If you are using XSLT 2.0 with XPath 2.0 try double apostrophe:
<xsl:when test="$trialSiteName = 'Physician''s Office'">
Look for a full explanation by Dimitre Novatchev in his answer Escape single quote in xslt concat function
in between " you can add what ever special characters you want.
<xsl:when test="$trialSiteName = "Physician's what ever special charactors plainly add Office"">

Prevent whitespaces between two XSLT-elements from being removed by the xslt-processor

my question is a bit different from the other ones..
i got an xsl-code like this:
<xsl:value-of select="..."/> <xsl:value-of select="...">
what i want in my result is:
result_of_select_1 result_of_select_2
what i get is:
result_of_select_1result_of_select_2
how can i prevent this? ( any xsl:output option for example? )
All the other solutions i found were specificly for the same problem but in the XML-Source document and not in the XSLT-document like this one...
btw. a solution like "insert elements instead of the spaces" is not a possible solution for my, because the xslt-code is generated dynamically
thanks in advance
The white space as you have it is insignificant and gets discarded. If that was not the case, every last bit of white space you have in your XSLT code would end up in the result document. You must be explicit about the white space you want in the result.
User either:
<xsl:value-of select="concat(..., ' ', ...)" />
or:
<xsl:value-of select="..." />
<xsl:text> </xsl:text>
<xsl:value-of select="..." />
Use:
<xsl:value-of select="..."/><xsl:text> </xsl:text><xsl:value-of select="...">
EDIT:
Refer to this ASCII table for other symbols