Re: [xsl] Expanding Tabs

Subject: Re: [xsl] Expanding Tabs
From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
Date: Wed, 31 Mar 2010 10:26:35 +0200
Here is an alternative solution:

<xsl:function name="wl:spaces" as="xs:string">
  <xsl:param name="repeats" as="xs:integer"/>
  <xsl:choose>
    <xsl:when test="$repeats=0">  <!-- be more general -->
      <xsl:value-of select="''"/>
    </xsl:when>
    <xsl:when test="$repeats=1">
      <xsl:value-of select="' '"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="half" select="$repeats idiv 2"/>
      <xsl:value-of select="concat(wl:spaces($half),wl:spaces($repeats
- $half))"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<!-- tabify <pos> <head> <tail>
     Recurse, appending spaces according to <pos> and first <tail>
item to <head> -->
<xsl:function name="wl:tabify" as="xs:string">
  <xsl:param name="pos"  as="xs:integer"/>
  <xsl:param name="head" as="xs:string"/>
  <xsl:param name="tail" as="xs:string*"/>
  <xsl:choose>
    <xsl:when test="count($tail)=0">
      <xsl:value-of select="$head"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of
        select="wl:tabify($pos,concat($head,wl:spaces($pos -
string-length($head) mod $pos),$tail[1]),subsequence($tail,2))"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<!-- expand <str> <pos>
     Expand <str> inserting spaces for TABs, assuming tab stops apart
by <pos> chars. -->
<xsl:function name="wl:expand" as="xs:string">
  <xsl:param name="str" as="xs:string"/>
  <xsl:param name="pos" as="xs:integer"/>
  <xsl:choose>
    <xsl:when test="string-length($str)=0">
      <xsl:value-of select="''"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="tokens" as="xs:string+"
select="tokenize($str,'\t')"/>
      <xsl:value-of
select="wl:tabify($pos,$tokens[1],subsequence($tokens,2))"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<xsl:template name="expand">
<text>
<xsl:for-each select="tokenize(unparsed-text($textpath), '\r?\n')">
  <line><xsl:value-of select="wl:expand(.,8)"/></line>
</xsl:for-each>
</text>
</xsl:template>

Cheers
-W


On Tue, Mar 30, 2010 at 11:10 PM, Alex Muir <alex.g.muir@xxxxxxxxx> wrote:
>
> 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>&#x20;</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