xsl-list
[Top] [All Lists]

Re: [xsl] Wrapping around textual and element node at the same time

2010-09-09 03:59:42
Dear Gabor,

I suggest a two-pass process: first replace whitespace with an empty separator element, then group (either starting with the separator or adjecent non-separator nodes). See below.

-Gerrit

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  xmlns:saxon="http://saxon.sf.net/";
  version="2.0"
  exclude-result-prefixes="saxon xs"
>

  <xsl:output
    method="xml"
    indent="yes"
    saxon:suppress-indentation="seg"
    />

  <xsl:template match="@* | *" mode="#all">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" mode="#current"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="p">
    <xsl:copy>
      <xsl:variable name="separated" as="node()*">
        <xsl:apply-templates mode="insert-sep" />
      </xsl:variable>
<xsl:for-each-group select="$separated" group-adjacent="not(self::sep)">
        <xsl:if test="current-grouping-key()">
          <seg>
            <xsl:sequence select="current-group() except self::sep" />
          </seg>
        </xsl:if>
      </xsl:for-each-group>
      <!-- Alternative grouping, may create empty seg though:
      <xsl:for-each-group select="$separated" group-starting-with="sep">
        <seg>
          <xsl:sequence select="current-group() except self::sep" />
        </seg>
      </xsl:for-each-group>
      -->
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()" mode="insert-sep">
    <xsl:analyze-string select="." regex="\s+">
      <xsl:matching-substring>
        <sep/>
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:value-of select="." />
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:template>

</xsl:stylesheet>



On 09.09.2010 10:29, Gábor Tóth wrote:
Dear All,

I am trying to wrap around words in a paragraph which looks like this.


<text><p><gliph>T</gliph>his is a very short
parapraph.<gliph>I</gliph>t is also tricky.</p>  </text>


The result what I would like,  should look like this.

<text>
     <p>
         <seg><gliph>T</gliph>his</seg>
         <seg>is</seg>
         <seg>a</seg>
         <seg>very</seg>
         <seg>short</seg>
         <seg>parapraph.</seg>
         <seg><gliph>I</gliph>t</seg>
         <seg>is</seg>
         <seg>also</seg>
         <seg>tricky.</seg>
     </p>
</text>

For wrapping around all words I use this template.

<xsl:template match="text/p/text()">


         <xsl:analyze-string select="." regex="\w+">

             <xsl:matching-substring>


                 <seg><xsl:value-of select="."/></seg>

             </xsl:matching-substring>
             <xsl:non-matching-substring>
                 <xsl:value-of select="."/>
             </xsl:non-matching-substring>
         </xsl:analyze-string>
     </xsl:template>

However, this template wraps around only the textual nodes but cannot do this:

<seg><gliph>T</gliph>his</seg>.

Is there any way to wrap around a textual node and an element node together?

Or should I do the wrapping first and then moving the gliph element later?

Thanks,

Gabor

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail:<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--
Gerrit Imsieke
Geschäftsführer / Managing Director
le-tex publishing services GmbH
Weissenfelser Str. 84, 04229 Leipzig, Germany
Phone +49 341 355356 110, Fax +49 341 355356 510
gerrit(_dot_)imsieke(_at_)le-tex(_dot_)de, http://www.le-tex.de

Registergericht / Commercial Register: Amtsgericht Leipzig
Registernummer / Registration Number: HRB 24930

Geschäftsführer: Gerrit Imsieke, Svea Jelonek,
Thomas Schmidt, Dr. Reinhard Vöckler
----------------------------------------------------------------------
Besuchen Sie uns auf der Frankfurter Buchmesse
in Halle 4.2, Stand G446.

Mehr dazu unter www.le-tex.de/de/buchmesse.html

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>