I would like to include apostrophe in the output attributes - xslt

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>

Related

Escape apostrophe character in a list in XSLT

How can I use the apostrophe ' character in a list? The following code fails, because the list contains an unescaped character in word "Fruit's 13". I tried escape it by backslash character and also by ', but none of them worked.
<xsl:param name="unsorted-values" as="xs:string*" select="'Apple','Banana','Fruit's 13'"/>
<xsl:param name="values" as="xs:string*">
<xsl:perform-sort select="$unsorted-values">
<xsl:sort select="string-length()" order="descending"/>
</xsl:perform-sort>
</xsl:param>
In XPath 2 and later in string literal delimited by single quotes (apostrophs) you can double '' a single quote/apostroph to have it escaped inside the string value, most attributes of XSLT like select use XPath expressions so there you can use that syntax.
Of course in the context of XSLT/XML you could also use select="'Apple', 'Banana', "Fruit's 13"".
XPath 3.1 syntax section for that is in https://www.w3.org/TR/xpath-31/#prod-xpath31-StringLiteral.

XSLT - curly braces in regex

I have a text node from which I have to extract a particular sub string based on a pattern
Below is the actual string:
//Some_string,"some_string_1":"target_string"},some_other_string//
Following is the regex pattern I am trying to use:
<xsl:analyze-string select="text_node/text()" regex=",("some_string_1":.*?)"}">
<xsl:matching-substring>
<xsl:value-of select="substring-after(regex-group(1),':"')"/>
</xsl:matching-substring>
</xsl:analyze-string>
My extracted sub string should be "target_string"
But I am getting the following error
Fatal error during transformation using //my_path: Closing curly brace in attribute value template ",("some_string_1":.*?)"}" must be doubled;
I tried to use double curly braces also but didn't work
Note - I am using Ant script to run the XSLT with saxon-he-10.1.jar
Thanks in advance!
I tend to put regular expressions into an xs:string typed parameter or variable e.g.
<xsl:param name="pattern" as="xs:string">,("some_string_1":(.*?))"}</xsl:param>
then I can use
<xsl:analyze-string select="." regex="{$pattern}">
<xsl:matching-substring>
<xsl:value-of select="regex-group(2)"/>
</xsl:matching-substring>
</xsl:analyze-string>
where I have a lot less to worry about escaping things in an attribute value template like the regex attribute.
Note, that, if you use XSLT 3, it is safer to have
<xsl:param name="pattern" as="xs:string" expand-text="no">,("some_string_1":(.*?))"}</xsl:param>
to avoid any text value template setting higher up in the tree kicking in.
You need to double the curly bracket because it has special meaning in XSLT and escape it because it has special meaning in regex:
",("some_string_1":.*?)"\}}"

XSL - Replace pipe by another character

I use XSLT 2. How I can replace pipe by aonther character ?
For exemple I have an element like this :
<list items="A1|A2|A3"/>
I want to have
<list items="A1,A2,A3"/>
I tried something like this, but not working
<xsl:variable name="result" select="replace(list/#items, '|', ',')"/>
What is problem ?
The replace() function uses regex - and the pipe character is a special character in regex. Either escape the character:
<xsl:variable name="result" select="replace(list/#items, '\|', ',')"/>
or use the translate() function instead.

XSLT- Get substring and pass it as parameters to java function

In the below code snippet, I am trying to get the substring of my #imageMeta node, append some more path location and pass it as a parameter to my java method through XSLT.
<xsl:variable name="imagePathFrom" select="/config/assets/images/{substring-after(#imageMeta,'/')}" />
<xsl:variable name="imagePathTo" select="'/dev/svn_root/platform/system'" />
<xsl:value-of select="filecopy:copyFile($imagePathFrom, $imagePathTo)"/>
My #imageMeta node data looks like Images/common/dialog/dialogue_black.png.
I have to convert the above path to images/common/dialog/dialogue_black.png (note the change of capital 'I' to small 'i') and append some more path data.
So my final path entry should look like "/config/assets/images/common/dialog/dialogue_black.png". When i run my code snippet i get an error stating:
line 51: Error parsing XPath expression '/config/assets/images/{substring-after(#imageMeta,'/')}'.'
Please help.
<xsl:variable name="imagePathFrom" select="/config/assets/images/{substring-after(#imageMeta,'/')}" />
There are two problems here:
A syntax error -- a select is probably the only attribute attribute in XSLT that cannot contain an AVT.
Even without the AVT, this would attempt to select all /config/assets/images nodes, but the intent is that the variable must contain the string "/config/assets/images"
Solution to both problems:
<xsl:variable name="imagePathFrom" select=
"concat('/config/assets/images/', substring-after(#imageMeta,'/')" />
Alternative solution:
<xsl:variable name="imagePathFrom" select=
"concat('/config/assets/',
translate(substring(#imageMeta, 1, 1),
$vUpper,
$vLower
),
substring(#imageMeta, 2)
)" />
where $vLower and $vUpper are defined, respectively, as:
'abcdefghijklmnopqrstuvwxyz'
and
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
There is one problem in your code:
<xsl:variable name="imagePathFrom" select="/config/assets/images/{substring-after(#imageMeta,'/')}" />
It suppose to be ..
<xsl:variable name="imagePathFrom" select="substring-after(/config/assets/images/#imageMeta,'/')" />
infant programmer 'Aravind' suggestion will solve your parse error.
You also mentioned you wanted to lower-case the capital i. Two options here:
Using XSLT 1.0, this StackOverflow answer explains how to lower-case the first character of a string. It won't work for Unicode characters such as 'Í' but you probably don't need it.
XSLT 2.0 has a lower-case function, which will lower-case your entire string, and may not be what you're looking for.

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