Re: [xsl] question about apply-templates / following-sibling

Subject: Re: [xsl] question about apply-templates / following-sibling
From: Marko Petersen <lg002237@xxxxxxxxxxxxxxxx>
Date: Thu, 12 Dec 2002 16:46:45 +0100
Hi,

sorry for the previous mail, I tried it a long time and 30 minutes after posting
the mail I got the result I expected.


But maybe there is a better way to do this, because in the original DTD there are
some more elements than index to check for and so this will be a quite long select
statement (checking elements and following-sibling elements...). For example not
only section and simplesect, but also sect1, sect2 and so on, and not only index,
but also toc, lot, glossary and bibliography ...


I now use the following:

<xsl:template match="chapter">
   <xsl:copy>
      <xsl:apply-templates select="@*"/>

         <xsl:apply-templates select="*[
                     not(name() = 'section') and
                     not(name() = 'simplesect') and
                     not(name() = 'index')]
                     |   index[following-sibling::*[not(name() = 'index')]]"/>

<para>New para</para>

<xsl:apply-templates select="section | simplesect |
index[not(following-sibling::*[not(name() = 'index')])]"/>


   </xsl:copy>
</xsl:template>

Greetings,

Marko

At 15:58 12.12.2002 +0100, you wrote:
Hi,

I would like to update some XML documents by inserting an element.
The documents are vaild to the DocBook DTD, but the following is a
simple subset which shows the problem I have:

<!ELEMENT chapter
    (   beginpage?, chapterinfo?, title
        (index)*,
        (
          (
            (para | %chapter.elements; | beginpage)+,
          (section* | simplesect*)
          )   |
        (section+ | simplesect+)
        ),
        (index)*,
    )
>

The entity chapter.elements contains over 50 elements (but not index,
section or simplesect).
In my documents I want to insert a para element at the last position
where it is allowed. The document should still be valid. If the chapter contains
a section or simplesect, the para can be inserted right before it, but if not, it
has to be the last element of chapter, only followed by index elements if
present. For example:


<chapter>
  <beginpage pagenum="1">
  <title>test</title>
  <index>index stuff</index>
  <index>index stuff</index>
  <para>text1</para>
  <beginpage pagenum="2">
  <!-- the new para should be here -->
  <index>index stuff</index>
  <index>index stuff</index>
</chapter>

I tried to use apply-templates, but I do not know a good way to select the first
optional index elements without selecting the ones that could be at the end
of the chapter. I need something like "all index elements where only index elements
are following".


<xsl:template match="chapter">
   <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <!-- copy everything before new para, including index elements -->
      <para>New para</para>
      <!-- copy everything after new para, including index elements  -->
   </xsl:copy>
</xsl:template>

Any ideas how to do this? I tried something with following-sibling, but it does not work.

Thanks and greetings,

Marko


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




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


Current Thread