I have a string "aa::bb::aa"
and need to turn it in to "aa, bb, aa"
I have tried
translate(string,':',', ')
but this returns "aa,,bb,,aa"
How can this be done.
A very simple solution (that will work as long as your string value doesn't have spaces):
translate(normalize-space(translate('aa::bb::cc',':',' ')),' ',',')
translate ":' into " "
normalize-space() to collapse multiple whitespace characters into a single space " "
translate single spaces " " into ","
A more robust solution would be to use a recursive template:
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
You can use it like this:
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="'aa::bb::cc'"/>
<xsl:with-param name="replace" select="'::'" />
<xsl:with-param name="with" select="','"/>
</xsl:call-template>
You can use this
Syntax:- fn:tokenize(string,pattern)
Example: tokenize("XPath is fun", "\s+")
Result: ("XPath", "is", "fun")
Related
Why cant I use this XSLT string function in a template?
<xsl:with-param name="text" select="substring($text,'2') and substring($text,1,(string-length($text)-1))" />
Here is the template:
<!-- Template to remove double quotes if available in first and last position of any field -->
<xsl:template name="remove-quotes">
<xsl:param name="text"/>
<xsl:param name="quot" select="'"'"/>
<xsl:param name="trim1" select="substring($text,'2')"/>
<xsl:param name="trim2" select="substring($text,1,(string-length($text)-1))"/>
<xsl:choose>
<xsl:when test="starts-with($text,$quot) and ends-with($text,$quot)">
<xsl:call-template name="remove-quotes">
<xsl:with-param name="text" select="$trim1 and $trim2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Invoked by:
<xsl:call-template name="remove-quotes">
<xsl:with-param name="text" select="XXXXX"/>
</xsl:call-template>
I am not sure what your template is trying to do, but certainly this part makes no sense:
<xsl:call-template name="remove-quotes">
<xsl:with-param name="text" select="$trim1 and $trim2"/>
</xsl:call-template>
and is Boolean operator. An expression that contains and returns a result of either true() or false().
Same thing with:
<xsl:with-param name="text" select="substring($text,'2') and substring($text,1,(string-length($text)-1))" />
Added:
To remove either a leading or a trailing quote or both, you could do simply:
<xsl:variable name="lead" select="number(starts-with($text, '"'))" />
<xsl:variable name="trail" select="number(ends-with($text, '"'))" />
<xsl:value-of select="substring($text, 1 + $lead, string-length($text) - $lead - $trail)" />
XSLT Template:
<!-- Template to remove trailing and leading double quotes from the fields -->
<xsl:template name="remove-quotes">
<xsl:param name="text"/>
<xsl:param name="quot" select="'"'"/>
<xsl:param name="lead" select="number(starts-with($text, '"'))"/>
<xsl:param name="trail" select="number(ends-with($text, '"'))"/>
<xsl:choose>
<xsl:when test="starts-with($text,$quot) and ends-with($text,$quot)">
<xsl:call-template name="remove-quotes">
<xsl:with-param name="text" select="substring($text, 1 + $lead, string-length($text) - $lead - $trail)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Invoke like this:
<xsl:call-template name="remove-quotes">
<xsl:with-param name="text" select="SampleText"/>
</xsl:call-template>
I have a node in an XML file:
<TEST_STRING>12,13,12,14</TEST_STRING>
I need to count how many unique numbers/values this string has. For example, in this case there are 2 unique values i.e. 13 and 14.
Honestly speaking i could not build anything yet. It seems it is difficult in XSLT 1.0 but my system only supports 1.0.
Is there any solution for it?
If your processor supports a node-set extension function (either exslt or the Microsoft msxsl one) then you can do it in two steps, first split the string and build an XML fragment with one element per value, then use normal XPath techniques to find the singletons.
Step one can be done with a tail-recursive template:
<xsl:template name="splitString">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="contains($str, ',')">
<item><xsl:value-of select="substring-before($str, ',')"/></item>
<xsl:call-template name="splitString">
<xsl:with-param name="str" select="substring-after($str, ',')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<item><xsl:value-of select="$str"/></item>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
In the appropriate place you can call this as
<xsl:variable name="itemsRTF">
<xsl:call-template name="splitString">
<xsl:with-param name="str" select="TEST_STRING"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="items" select="exsl:node-set($itemsRTF)"/>
The items variable now contains a fragment of XML like
<item>12</item>
<item>13</item>
<item>12</item>
<item>14</item>
The next challenge is to find the singletons, which you can do with an expression like
$items/item[not(. = (preceding-sibling::item | following-sibling::item))]
(There are more efficient approaches using a key but for small numbers of items it's probably not worth the bother). So to count the singletons
<xsl:value-of select="count($items/item[not(. = (preceding-sibling::item | following-sibling::item))])"/>
It's possible without nodesets. It's not optimal, of course (hello, Shlemiel The Painter), but it's pretty easy - increment counter only if there is no given string ahead or behind.
Template
<xsl:template name="calcUnique">
<xsl:param name="str"/>
<xsl:param name="back"/>
<xsl:param name="count"/>
<xsl:if test="$str">
<xsl:choose>
<xsl:when test="contains($str, ',')">
<xsl:variable name="part" select="substring-before($str, ',')"/>
<xsl:call-template name="calcUnique">
<xsl:with-param name="str" select="substring-after($str, ',')"/>
<xsl:with-param name="back" select="concat($back, ',', $part)"/>
<xsl:with-param name="count">
<xsl:choose>
<xsl:when test="contains(concat($str, ','), concat(',', $part, ',')) or contains(concat($back, ','), concat(',', $part, ','))">
<xsl:value-of select="$count"/>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$count + 1"/></xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="contains(concat($back, ','), concat(',', $str, ','))">
<xsl:value-of select="$count"/>
</xsl:when>
<xsl:otherwise><xsl:value-of select="$count + 1"/></xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
Sample usage
<xsl:variable name="result">
<xsl:call-template name="calc">
<xsl:with-param name="str" select="TEST_STRING"/>
<xsl:with-param name="back" select="''"/>
<xsl:with-param name="count" select="0"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$result"/>
I have a XSL which I am using to transform one XML to another like:
<xsl:value-of select="somestatement"/>
where some statement has single quotes and double quotes. Ex: This is an "example" string. I have 'single quotes'
I want to replace single quotes with ' and double quotes with " so that output string will be :
This is an "example" string. I have 'single quotes'
Can someone please suggest a solution for this?
Thanks for the help in advance.
You need to call a named recursive template for this. Try:
<xsl:template name="escape-quotes">
<xsl:param name="text"/>
<xsl:param name="searchString">'</xsl:param>
<xsl:param name="replaceString">'</xsl:param>
<xsl:variable name="apos">'</xsl:variable>
<xsl:choose>
<xsl:when test="contains($text,$searchString)">
<xsl:call-template name="escape-quotes">
<xsl:with-param name="text" select="concat(substring-before($text,$searchString), $replaceString, substring-after($text,$searchString))"/>
<xsl:with-param name="searchString" select="$searchString"/>
<xsl:with-param name="replaceString" select="$replaceString"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$searchString=$apos">
<xsl:call-template name="escape-quotes">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="searchString">"</xsl:with-param>
<xsl:with-param name="replaceString">"</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" disable-output-escaping="yes"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Example of calling the template:
<output>
<xsl:call-template name="escape-quotes">
<xsl:with-param name="text">This is an "example" string. I have 'single quotes'.</xsl:with-param>
</xsl:call-template>
</output>
Result:
<output>This is an "example" string. I have 'single quotes'.</output>
I am stuck with XSLT 1.0 making a hyphenation script for verbatim output to PDF. (note it is PDF so I can't use clever CSS to hellp me out.
I would like to be able to access both text() nodes and child text() nodes to compare stringlength as part of the script.
For example:
<programlisting>This text will be displayed without word-wrap
I can measure length of string between end line chars '
'
and break long lines. That works great until
<emphasis>this gets added</emphasis> then my counting loop doesn't
count the text in the emphasis tags.
</programlisting>
I am looping through the text() using a template called screen. It detects the end of line character and decides if the line is too long. Long lines are broken and the remainder recursed through the template until the remainder is less than the max line length... then onto the next line of text...
... that works great, but now i have some children in the and I can't figure out how I can access the text() and any child node's text() at the same time to calculate string length.
Sorry for the long example code...
The template to match looks like this...
<xsl:template match="programlisting/text()">
<xsl:variable name="max_width">100</xsl:variable>
<xsl:variable name="min_width">80</xsl:variable>
<xsl:call-template name="screen">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="max_width" select="$max_width"/>
<xsl:with-param name="min_width" select="$min_width"/>
</xsl:call-template>
</xsl:template>
The template to decide if string before linebreak is too long:
<xsl:template name="screen">
<xsl:param name="text" select="."/>
<xsl:param name="max_width"/>
<xsl:param name="min_width"/>
<xsl:variable name="fixed_text"> <!-- add end linebreak if missing -->
<xsl:choose>
<xsl:when test="substring($text,string-length($text),1) = '
'">
<xsl:value-of select="$text"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($text,'
')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="first_line">
<xsl:value-of select="substring-before(concat($fixed_text,'
'),'
')"/>
</xsl:variable>
<xsl:variable name="other_lines">
<xsl:value-of select="substring-after($fixed_text,concat($first_line,'
'))"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="string-length(normalize-space($first_line)) < 1"><!-- blank line (just trim and copy)-->
<xsl:value-of select="concat($verbatim_padding,substring($first_line,1,100),'
')"/>
</xsl:when>
<xsl:when test="string-length($first_line) < 100"> <!-- short line (just copy)-->
<xsl:value-of select="concat($verbatim_padding,$first_line,'
')"/>
</xsl:when>
<xsl:otherwise>
<!-- Line is too long!! -->
<xsl:variable name="wrapped_lines">
<xsl:call-template name="break_line">
<xsl:with-param name="long_string" select="$first_line"/>
<xsl:with-param name="max_chars" select="100"/>
<xsl:with-param name="min_chars" select="80"/>
<xsl:with-param name="break_chars" select="' ,/;'"/>
<xsl:with-param name="linebreak_string" select="'~'"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="concat($verbatim_padding,$wrapped_lines)"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length($other_lines) > 0">
<xsl:call-template name="screen">
<xsl:with-param name="text" select="$other_lines"/>
<xsl:with-param name="max_width" select="$max_width"/>
<xsl:with-param name="min_width" select="$min_width"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
The template to break up long lines:
<xsl:template name="break_line">
<xsl:param name="long_string"/>
<xsl:param name="max_chars"/> <!--max chars allowed on a line -->
<xsl:param name="min_chars"/> <!--max char position foa soft linebreak (else we hard break at the max chars!) -->
<xsl:param name="break_chars"/> <!-- chars used for soft breaking -->
<xsl:param name="linebreak_string"/> <!-- add this to end of a broken line -->
<xsl:choose>
<xsl:when test="(string-length($long_string) < $max_chars) or (string-length($long_string) < $min_chars) or (string-length($long_string) < 1)">
<xsl:value-of select="concat($long_string,'
')"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="trim_x_by">
<xsl:call-template name="CheckLastChar">
<xsl:with-param name="string" select="$long_string"/>
<xsl:with-param name="start" select="$max_chars"/>
<xsl:with-param name="stop" select="$min_chars"/>
<xsl:with-param name="break_chars" select="$break_chars"/>
<xsl:with-param name="max_chars" select="$max_chars"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="first_x_chars"><xsl:value-of select="substring($long_string,1,$trim_x_by)"/></xsl:variable>
<xsl:variable name="remaining_chars"><xsl:value-of select="substring-after($long_string,$first_x_chars)"/></xsl:variable>
<xsl:value-of select="concat($first_x_chars,' ',$linebreak_string)"/>
<xsl:call-template name="break_line">
<xsl:with-param name="long_string" select="$remaining_chars"/>
<xsl:with-param name="max_chars" select="$max_chars"/>
<xsl:with-param name="min_chars" select="$min_chars"/>
<xsl:with-param name="break_chars" select="$break_chars"/>
<xsl:with-param name="linebreak_string" select="$linebreak_string"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And finally the recursive checking of string chars to see if its a safe place to line-break (soft break)
<xsl:template name="CheckLastChar">
<xsl:param name="string"/>
<xsl:param name="stop" select="1"/>
<xsl:param name="start" select="string-length($string)"/>
<xsl:param name="count" select="$start"/>
<xsl:param name="break_chars"/>
<xsl:param name="max_chars"/>
<xsl:choose>
<xsl:when test="($count < $stop) or (string-length($string) < $stop)">
<!-- gone so far into the line that a linebreak would look weird! So return the max-length and that will
force a 'hard' linebreak exactly at the max-length point regardless of the character -->
<xsl:value-of select="$max_chars"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="last_char"><xsl:value-of select="substring($string,$count,1)"/></xsl:variable>
<xsl:choose>
<xsl:when test="contains($break_chars,$last_char)">
<xsl:value-of select="$count"/>
</xsl:when>
<xsl:otherwise>
<!-- keep looking -->
<xsl:call-template name="CheckLastChar">
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="stop" select="$stop"/>
<xsl:with-param name="start" select="$start"/>
<xsl:with-param name="count" select="$count - 1"/>
<xsl:with-param name="break_chars" select="$break_chars"/>
<xsl:with-param name="max_chars" select="$max_chars"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
To get all of the text in an element (including its descendants' text), just use string(Element):
<xsl:template match="programlisting">
<xsl:variable name="max_width">100</xsl:variable>
<xsl:variable name="min_width">80</xsl:variable>
<xsl:call-template name="screen">
<xsl:with-param name="text" select="string(.)"/>
<xsl:with-param name="max_width" select="$max_width"/>
<xsl:with-param name="min_width" select="$min_width"/>
</xsl:call-template>
</xsl:template>
Note the removal of text() from the match attribute.
XPath Spec
The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order.
The string() function converts an object to a string as follows:
A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.
I need to programatically replace "regular double quotes" with “typographer's quotes”
My initial thought is something like this:
<xsl:variable name="text">
<xsl:call-template name="replace-string"><!-- FYI: replace-string is a custom method that works like you would expect-->
<xsl:with-param name="text" select="."/>
<xsl:with-param name="replace" select="string(' "')" /><!-- left quote because of space before -->
<xsl:with-param name="with" select="string('“')"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="text2">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="string('" ')" /><!-- right quote because of space after -->
<xsl:with-param name="with" select="string('”')"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="$text2" />
My worry is situations where there is not a determining space by the quote. Such as these.
They say "this is great".
I like tigers ("big large cats").
Has anyone ever had to do this before that knows some extra rules to apply or a different strategy?
Thanks!
A solution that works without extension functions would be:
<xsl:template name="typograpic-quotes">
<xsl:param name="text" select="''" />
<xsl:param name="quote" select="'"'" />
<xsl:param name="open" select="'“'" />
<xsl:param name="close" select="'”'" />
<xsl:param name="inquote" select="false()" />
<xsl:choose>
<xsl:when test="contains($text, $quote)">
<xsl:value-of select="substring-before($text, $quote)" />
<xsl:choose>
<xsl:when test="$inquote">
<xsl:value-of select="$close" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$open" />
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="typograpic-quotes">
<xsl:with-param name="text" select="substring-after($text, $quote)" />
<xsl:with-param name="quote" select="$quote" />
<xsl:with-param name="open" select="$open" />
<xsl:with-param name="close" select="$close" />
<xsl:with-param name="inquote" select="not($inquote)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Needless to say that unbalanced quotes in the input will make this fail.
Regular Expressions.
regex:replace($textVariable, '"([^"]*)"' , 'gi', '“$1”')
Havent tested this, but this is an easy approach IMHO. you match all "(anything not ")zero or more times" and replace that with your other typographical quotes. The $1 is a back-reference to the first match. While there are issues here such as incorrectly nested text, quotes that are not closed etc., I am suggesting something like this. You may rewrite the regex for this one and test it out.
This is assuming that your XSLT processor supports the EXSLT extensions.
Because we have some unbalanced quotes. I am working on something a little more pragmatic. The below seems to work in all cases I have tried.
<!-- Get text. All quotes defaulted to right quote -->
<xsl:variable name="text">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="."/>
<xsl:with-param name="replace" select="string('"')" />
<xsl:with-param name="with" select="string('”')"/>
</xsl:call-template>
</xsl:variable>
<!-- Turn quotes preceded by a space into left quote -->
<xsl:variable name="text2">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="replace" select="string(' ”')" />
<!-- right quote because of space after -->
<xsl:with-param name="with" select="string(' “')"/>
</xsl:call-template>
</xsl:variable>
<!-- Turn quotes preceded by a parenthesy into left quote -->
<xsl:variable name="text3">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$text2"/>
<xsl:with-param name="replace" select="string('(”')" />
<!-- right quote because of space after -->
<xsl:with-param name="with" select="string('(“')"/>
</xsl:call-template>
</xsl:variable>
<!-- Turn quotes that are the first character in the text into left quote -->
<!-- Note: this one is still a little funky. For some reason the first character is always whitespace. So I am checking the second character because it is really the first. -->
<xsl:variable name="text4" >
<xsl:choose>
<xsl:when test="normalize-space(substring( $text3, 2, 2 )) = string('”')">
<xsl:value-of select="string('“')"/>
<xsl:value-of select="substring($text3, 3)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text3"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:apply-templates select="$text4" />