xsl-list
[Top] [All Lists]

RE: read/write in the same xml file

2003-06-11 04:30:16
Hi,

this is an example for what i want to do:

i supose this is my xml file:
<document>
<para>
<word>aaa</word>
<word>bbb</word>
<word>ccc</word>
<word>ddd</word>
<word>eee</word>
</para>
<para>
<word>kkk</word>
<word>fff</word>
<word>mmm</word>
<word>ppp</word>
</para>
</document>

my output file should have this structure:

table of contents:
paragraph1  12    32
paragraph2  44  59

paragraph1
aaa
bbb
ccc
ddd
eee
paragraph2
kkk
fff
mmm
ppp


so  the first paragraph begin in character 12 and finish in 
character 32
in the output file.

  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="document">
    <xsl:text>table of contents:&#xA;</xsl:text>
    <xsl:call-template name="counter" />
    <xsl:text>&#xA;</xsl:text>
    <xsl:for-each select="para">
      <xsl:text>paragraph</xsl:text>
      <xsl:value-of select="position()"/>
      <xsl:text>&#xA;</xsl:text>
      <xsl:for-each select="word">
        <xsl:value-of select="."/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="counter">
    <xsl:param name="offset" select="0"/>
    <xsl:param name="node" select="para[1]" />
    <xsl:param name="count" select="1"/>
    <xsl:text>paragraph</xsl:text>
    <xsl:value-of select="$count"/>
    <xsl:text>  </xsl:text>
    <xsl:variable name="start" select="$offset + 11 + 
string-length(string($count))"/>
    <xsl:value-of select="$start"/>
    <xsl:text>  </xsl:text>
    <xsl:if test="$count = 1">
      <xsl:text>  </xsl:text>
    </xsl:if>
    <xsl:variable name="end" select="$start + count($node/word) + 
boolean($node/following-sibling::para) - 1 + string-length($node)"/>
    <xsl:value-of select="$end"/>
    <xsl:text>&#xA;</xsl:text>
    <xsl:if test="$node/following-sibling::para">
      <xsl:call-template name="counter">
        <xsl:with-param name="offset" select="$end"/>
        <xsl:with-param name="node" select="$node/following-sibling::para[1]"/>
        <xsl:with-param name="count" select="$count + 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

Feels really silly to write stylesheets like this...

Cheers,

Jarno - Conjure One: Tears From The Moon

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list