|
Subject: Re: [xsl] Expanding Tabs From: Alex Muir <alex.g.muir@xxxxxxxxx> Date: Tue, 30 Mar 2010 21:10:12 +0000 |
Hi,
Wrote some code that does the job for the files we are using.
Basically looks for a line containing a tab then splits the line
recursively by the text before the tab, the tab and the text following
the tab and calculates the number of spaces needed using some modulus.
A maxTabSize variable is set to 8 as a parameter in the style sheet
given this is what is the maximum number of spaces in our documents in
both textpad and notepad for a tab.
I tested it and couldn't break it however could be buggy given just wrote it.
Regards
<xsl:template match="/" name="start">
<xsl:element name="document">
<xsl:variable name="regex">
<xsl:text>([^\t]*?)(\t)(.*?$)</xsl:text>
</xsl:variable>
<xsl:element name="content">
<xsl:element name="text">
<xsl:for-each select="tokenize(unparsed-text($input_uri,
'UTF-8'), '\r?\n')">
<xsl:choose>
<xsl:when test="matches(.,$regex)">
<xsl:call-template name="expandTabs">
<xsl:with-param name="maxTabSize"
select="xs:integer($maxTabSize)"/>
<xsl:with-param name="content" select="."/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template name="expandTabs">
<xsl:param name="maxTabSize" as="xs:integer"/>
<xsl:param name="content"/>
<xsl:variable name="regex">
<xsl:text>([^\t]*?)(\t)(.*?$)</xsl:text>
</xsl:variable>
<xsl:choose>
<xsl:when test="string-length($content) > 0">
<xsl:analyze-string select="$content" regex="{$regex}">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<xsl:variable name="lenghtBeforeTabStop"
select="string-length(regex-group(1)) mod $maxTabSize"
as="xs:integer"/>
<xsl:call-template name="insertSpace">
<xsl:with-param name="n" select="$maxTabSize -
$lenghtBeforeTabStop"/>
<xsl:with-param name="i" select="1"/>
</xsl:call-template>
<xsl:choose>
<xsl:when test="matches(regex-group(3),$regex)">
<xsl:call-template name="expandTabs">
<xsl:with-param name="maxTabSize" select="$maxTabSize"/>
<xsl:with-param name="content" select="regex-group(3)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="regex-group(3)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="insertSpace">
<xsl:param name="i" as="xs:integer"/>
<xsl:param name="n" as="xs:integer"/>
<xsl:choose>
<xsl:when test="$i le $n">
<xsl:text> </xsl:text>
<xsl:call-template name="insertSpace">
<xsl:with-param name="n" select="$n"/>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
On Tue, Mar 30, 2010 at 2:18 PM, Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
wrote:
> Tab stops are always by convention and rarely contained in a data file.
>
> At least that's my experience with generations of text editors and
> frequent anger due to seeing weird alignment after switching editors.
>
> -W
>
> On Tue, Mar 30, 2010 at 4:00 PM, Dave Pawson <davep@xxxxxxxxxxxxx> wrote:
>>
>> On Tue, 30 Mar 2010 14:08:26 +0100
>> "Michael Kay" <mike@xxxxxxxxxxxx> wrote:
>>
>> > >
>> > > What I'm doing is converting old text documents into XML.
>> >
>> > So why not run a text-based utility on them to convert tabs into
>> > spaces, before you do anything else?
>>
>> Isn't the line length, tab-stop setting etc missing from the XML?
>> Even with text pre-processing I'm not sure how this can be done?
>>
>> guesswork?
>>
>>
>> regards
>>
>>
>>
>> --
>>
>> regards
>>
>> --
>> Dave Pawson
>> XSLT XSL-FO FAQ.
>> http://www.dpawson.co.uk
>
>
--
Alex
https://sites.google.com/a/utg.edu.gm/alex
Some Good Music
http://sites.google.com/site/greigconteh/
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] Expanding Tabs, Wolfgang Laun | Thread | Re: [xsl] Expanding Tabs, Wolfgang Laun |
| Re: [xsl] Create Table, bernie bonn | Date | RE: [xsl] Calculating Column Total, Shashank Jain |
| Month |