xsl-list
[Top] [All Lists]

Re: [xsl] Display text and all the following nodes except <anchor> and <pb>

2008-09-19 04:26:10

I don't think you have said whether you are working with xslt 1 or 2,
grouping problems are usually coded rather differently in xslt 2.

Some comments on your code.

XSL:
<xsl:template match="doc">

It's really a lot better if you post _complete_ but small stylesheets,
then people can run the stylesheet and comment on the result. If you
only post f ragments, then people can't run them, and often the problem
turns out to be in the part of the code not posted.

Similarly

INPUT:
It's best if the input you post is well formed so peole can test
proposed answers against it.



 <xsl:template match="doc">
 <ul>
  <xsl:for-each select="//docGroup


This is slightly strange (and potentially expensive) as it's saying for
each doc element in the document, ignore the current context, go back to
the root of the document and then search the entire document to
arbitrary depth looking for docGroup elements and then search to
arbitrary depth inside them for div elemenets (which apparently takes
you back into the doc elements where you started)/


Generally it's best to avoid using //.

<xsl:template match="div/head">
    <xsl:choose>

A template that just consists of a xsl:when is usually more simply
written as separate templates. In this case

<xsl:template match="div/head"/>
<xsl:template match="div[(_at_)type='ss1']/head" priority="2">
 <h2>
             <xsl:apply-templates/>
          </h2>
</xsl:template>


    <xsl:apply-templates select="*[not(name(.)='anchor' or 
name(.)='pb')]|text()"/>

As has been said previously testing on name() is error prone (if
namespaces are used) and potentialy inefficient (always).

    <xsl:apply-templates select="*[not(self::anchor or self::pb)]|text()"/>

would be better, but better still would be

   <xsl:apply-templates mode="toc"/>


together with

<xsl:template matcj="anchor|pb" mode="toc"/>




anyway if i make your input well formed then the following stylesheet
produces this which seems more or less as you asked for.



$ saxon dg.xml dg.xsl
<?xml version="1.0" encoding="utf-8"?>
<ul>
   <li>
      <a href="#sec1">O<span class="sCap">FFICERS</span> of the R<span 
class="sCap">EGIMENT</span> of 
I<span class="sCap">NFANTRY</span>
      </a>
   </li>
   <li>
      <a href="#sec2">
(1) Extract of a Letter from 
Governor William Blount to the Secretary of State
</a>
   </li>
</ul>
<h2 id="sec1">O<span class="sCap">FFICERS</span> of the R<span 
class="sCap">EGIMENT</span> of 
I<span class="sCap">NFANTRY</span>
</h2>
<h2 id="sec2">
(1) Extract of a Letter from 
Governor William Blount to the Secretary of State
</h2>










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

<xsl:output indent="yes"/>

<xsl:template match="docGroup">
 <xsl:apply-templates select="doc"/>
</xsl:template>

<xsl:template match="doc">
 <xsl:apply-templates select="docBody" mode="toc"/>
 <xsl:apply-templates select="docBody"/>
</xsl:template>

<xsl:template match="docBody" mode="toc">
 <ul>
  <xsl:apply-templates select="div[(_at_)type='ss1']" mode="toc"/>
 </ul>
</xsl:template>

<xsl:template match="div[(_at_)type='ss1']" mode="toc">
 <li>
  <a href="#sec{position()}">
   <xsl:apply-templates select="head"/>
  </a>
 </li>
</xsl:template>

<xsl:template match="sCap">
 <span class="sCap">
  <xsl:apply-templates/>
 </span>
</xsl:template>



<xsl:template match="docBody">
  <xsl:apply-templates select="div[(_at_)type='ss1']"/>
</xsl:template>

<xsl:template match="div[(_at_)type='ss1']">
 <h2 id="sec{position()}">
  <xsl:apply-templates select="head"/>
 </h2>
 <xsl:apply-templates select="*[not(self::head)]"/>
</xsl:template>

</xsl:stylesheet>


________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

--~------------------------------------------------------------------
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>
--~--