Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Counting occurences of a character in a string

From: "Dimitre Novatchev" <dnovatchev@--------->
To:
Date: 4/2/2008 5:33:00 AM
Here are three different ways. The first is the shortest and probably
the fastest. The second uses Dr. Kay's "double-translate" method.

The third illustrates how in FXSL one can glue-up functions to produce
quick solutions.

Please, do note, that the character to search for is contained in a
variable -- it seems to me that the solutions offered so far all used
the fixed character "X". They would have to build an RegEx expression
dynamically if this character was not known at compile time.

This transformation:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="f xs"
 >

 <xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/func-map.xsl"/>
 <xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/func-compose.xsl"/>
 <xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/func-dvc-foldl.xsl"/>
 <xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/func-Operators.xsl"/>
 <xsl:import href="C:/CVS-DDN/fxsl-xslt2/f/func-standardXpathFunctions.xsl"/>

 <xsl:output method="text"/>

  <xsl:variable name="vS" as="xs:string"
    select="'abbccddd e fgggggi ggg kk l'"/>

  <xsl:variable name="vX" as="xs:string"
   select="'g'"/>

  <xsl:variable name="vcpX" as="xs:integer"
   select="string-to-codepoints('g')"/>

 <xsl:template match="/">
  <xsl:sequence select=
   "count(string-to-codepoints($vS)[. eq $vcpX])"
  />

  <xsl:sequence select=
      "string-length(translate($vS,translate($vS,$vX,''),''))"
  />

  <xsl:sequence select=
      "f:foldl(f:add(),
               0,
               f:map(f:compose(f:number(), f:eq($vcpX)),
                     string-to-codepoints($vS)
                     )
               )"
  />


 </xsl:template>
</xsl:stylesheet>

produces this result:


8 8 8



-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play





On Tue, Apr 1, 2008 at 3:01 PM, Christian Roth <roth@xxxxxxxxxxxxxx> wrote:
> Hello,
>
> in XSLT 2, is there an easier or more efficient way to count the
> occurrences of a certain character (here: 'X') in a string $s than
>
> string-length( string-join( tokenize( $s, "[^X]+" ), "" ) )
>
> ?
>
> I'm sure there must be, I just don't see it at the moment...
>
> -Christian


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent