xsl-list
[Top] [All Lists]

Re: Selecting the first sentence of a paragraph

2003-10-28 01:06:43
This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

 <xsl:output omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="p" mode="fstSent"/>
  </xsl:template>

  <xsl:template match="@* | node()" mode="fstSent">
    <xsl:choose>
      <xsl:when test="not(self::p)
                     and
                      preceding::text()
                        [generate-id(ancestor::p[1])
                        =
                         generate-id(current()/ancestor::p[1])
                        ]
                        [contains(., '.')]"/>

      <xsl:when test="self::text()[contains(., '.')]">
        <xsl:value-of
        select="concat(substring-before(., '.'), '.')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"
                               mode="fstSent"/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

when applied on this source xml (a complicated variant of the one originally
provided):

<p> Identical
  <u>and similar</u>
  <b>
    <i>to</i>
    <keyword>systat</keyword>.
   The optional argument
  </b>
  <arg> n</arg> specifies the level of detail.
</p>

produces the wanted result:

<p> Identical
  <u>and similar</u>
  <b>
    <i>to</i>
    <keyword>systat</keyword>.</b></p>



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




"Larry Kollar" <kollar(_at_)alltel(_dot_)net> wrote in message
news:20954934-08FD-11D8-BF20-000A9585B842(_at_)alltel(_dot_)net(_dot_)(_dot_)(_dot_)
Subject says it mostly, but I want to include any elements as well as
text nodes making up that first sentence.

Hm, maybe I'd better show a sample XML fragment:

<section>
   <label>show exception</label>
   <p>Syntax: <cmd>show exception <arg>n</arg></cmd></p>
   <p>Identical to <keyword>systat</keyword>.
   The optional argument <arg>n</arg> specifies the level of detail.</p>
   <other>stuff we don't care about</other>
</section>

The part I have working pretty well applies the identity transform to
the first paragraph if it starts with "Syntax." I also want to copy the
first sentence of the next paragraph (or the first sentence of the first
paragraph if there's no "Syntax" definition). The following template
gets the first sentence but removes the internal markup:

<xsl:template match="p" mode="firstsentence">
<p><xsl:copy-of select='concat(substring-before(.,"."), ".")' /></p>
</xsl:template>

Producing "Identical to systat." That I expect, and set it up as a
placeholder until I could get what I really wanted. That's been
somewhat knottier.

I assume I'll have to use xsl:for-each to select all the nodes up
to the first period in the paragraph, apply the identity transform
to them, then select the node containing the period and use the
concat(substring-before(.,"."),".") function to get that part.

I've tried a couple of variations on
xsl:for-each select='./*/preceding-sibling::*[contains(text(),".")]'
which is probably causing hysterical laughter and/or retching
among some of you.... I'm probably making this harder than it
has to be.

Any suggestions are much appreciated. I'm using xsltproc if
that makes a difference.
--
Larry Kollar    k  o  l  l  a  r  @  a  l  l  t  e  l  .  n  e  t
"The hardest part of all this is the part that requires thinking."
-- Paul Tyson, on xml-doc


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






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



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