Re: [xsl] XSLT Text Processing: Fun with Anagrams

Subject: Re: [xsl] XSLT Text Processing: Fun with Anagrams
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Mon, 23 Apr 2007 14:27:33 -0700
On 4/22/07, Rashmi Rubdi <rashmi.sub@xxxxxxxxx> wrote:
Dimitre and everyone,

It's going to take me longer to understand parts of the anagram
solution because I've just begun learning version 2.0, but your
solution definitely shows that XSLT/XPath is capable of being applied
to much wider variety of problem domains besides transforming XML to
(X)HTML. The anagram solution definitely inspires one to explore more
of XSLT.

Rashmi,


The algorithm uses the simple fact that words that are anagrams of
each other have the same bag (not exactly "set")  of letters, the
canonical representation of which (their sorted sequence) is the
anagram key.

I order to understand the implementation one only needs to understand
and have experience with the <xsl:key .../> instruction and the key()
function.

I am also using two FXSL sorting functions -- f:qsort()   -- a very
generic and polymorphic sort, but slow in some cases; and the much
faster f:xsltSort(), which is a wrapper around the <xsl:perform-sort
.../> XSLT 2.0 instruction.  The current version of f:xsltSort()
accepts as one of its parameters a sequence of functions, each of
which generates one part of the sort key and then all such sort-key
parts are concatenated from left to right to form the complete sort
key.

The type of the sort key and its generated parts are assumed to be
xs:string. This is why I am using an auxiliary function to convert a
number to a right-justified fixed-length string, so that numbers will
be sorted correctly by this sorting function.  And if you are
wondering where do I need to sort integers in this solution, you are
quite right -- it is still dificult for people experienced in
XPath/XSL1.0 to realize that there are individual characters
(code-points) and to perceive them as ... integers.



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



I primarily used version 1.0 of XSLT/XPath for just transforming XML to (X)HTML, but now with version 2.0 there seem to be so many more possiblities. I'm sure they were possible with version 1.0 as well.

I am also simultaneously going through the example of counting word
frequencies which is in the XSLT 2.0 reference book by Michael Kay-
and it was a very interesting exercise, that shows one of the
applications of XSLT. It is also mentioned in the book that the same
solution in XSLT 1.0 took about 60 lines of code, where as with XSLT
2.0 it takes about 5 lines of code. That is a significant achievement.

~~~~

Thank you everyone for providing/ making suggestions on XML/ XSLT
syntax highligthing tools.

Sorry I wasn't very clear about the purpose of the XSLT syntax
highlighter tool, the purpose is mainly to present code on an HTML web
page.

The formatting shown here:
http://utfx-doc.sourceforge.net/manual.html#sect2 looks good, also its
nice to have a Unit Testing framework for XSLT. I'm familiar with
JUnit, thank you for the unit testing tool for XSLT.

> there is Saxon extension XSLTHL (http://sourceforge.net/projects/xslthl)
 looks interesting, I haven't tried docbook, but I will keep it in mind.

Yesterday, I also came accorss a plug-in called Code2HTML within the
jEdit editor (which supports about 130 programming lanugages),
basically it HTMLizes any code in the editor. I tired it and it works
well on XSLT/XML also -- in addition to highligting the syntax it also
generates a left margin with line numbers -- all in HTML.

I think Code2HTML a nice tool to present code in one programming
language on the same web page. The challenge arises when presenting
code from a collection of different programming languages because, a
CSS file is generated and the class of the syntax highlighting code
can conflict between languages. One solution may be to use an iFrame
to display snippets from different languages. But anyway, this
discussion on multiple programming languages is out of the scope of
this list :-)  -- I normally write solutions in a few languages Java,
HTML, XSLT/XPath, SQL etc.

-Regards
Rashmi

Current Thread