Print

Print


Hi,

I'm not sure I understand the requirement, but try this:

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

  <xsl:template match="div">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="head">
    <xsl:apply-templates select=".." mode="number"/>
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="body/div" mode="number"/>

  <xsl:template match="body/div/div" mode="number">
    <xsl:number level="any" count="body/div/div"
      format="1."/>
  </xsl:template>

  <xsl:template match="div/div/div" mode="number">
    <xsl:apply-templates select="parent::div" mode="number"/>
    <xsl:number level="single" format="1."/>
  </xsl:template>

</xsl:stylesheet>

It could be adjusted to use the @rend attribute instead of the
structure, if you liked.

Cheers,
Wendell

On Tue, Oct 30, 2012 at 5:44 AM, Sebastian Rahtz
<[log in to unmask]> wrote:
> This was surprisingly hard to code, which makes me think I am missing a trick.
> If we take this input
>
>     <body>
>       <div rend="excession">
>         <head>Part 1</head>
>         <div>
>           <head>Section 1</head>
>           <div>
>             <head>Section 1.1</head>
>           </div>
>           <div>
>             <head>Section 1.2</head>
>           </div>
>         </div>
>         <div>
>           <head>Section 2</head>
>         </div>
>       </div>
>       <div rend="excession">
>         <head>Part 1</head>
>         <div>
>           <head>Section 3</head>
>           <div>
>             <head>Section 3.1</head>
>           </div>
>           <div>
>             <head>Section 3.2</head>
>           </div>
>         </div>
>         <div>
>           <head>Section 4</head>
>         </div>
>       </div>
>     </body>
>
> then the best XSL I have been able to devise looks like this:
>
>   <xsl:template match="div">
>     <xsl:variable name="number">
>       <xsl:choose>
>         <xsl:when test="parent::div[@rend='excession']">
>           <xsl:number from="body" level="any" count="div[parent::div/@rend='excession']"/>
>         </xsl:when>
>         <xsl:when test="ancestor::div[@rend='excession']">
>           <xsl:number from="body" level="any" count="div[parent::div/@rend='excession']"/>
>           <xsl:text>.</xsl:text>
>           <xsl:number from="div[not(@rend='excession')]" count="div[not(@rend='excession')]" level="multiple"/>
>         </xsl:when>
>         <xsl:when test="@rend='excession'">
>           <xsl:number format="A"/>
>         </xsl:when>
>         <xsl:otherwise>
>           <xsl:number level="multiple"/>
>         </xsl:otherwise>
>       </xsl:choose>
>       <xsl:text>: </xsl:text>
>       <xsl:value-of select="head"/>
>     </xsl:variable>
>     <xsl:message>Processing <xsl:value-of select="$number"/></xsl:message>
>     <xsl:apply-templates/>
>   </xsl:template>
>
> Can anyone do better?
>
> Unfortunately, you cannot have a nicely styled <p> between <div> to put in the headings of Part 1 etc.
> --
> Sebastian Rahtz
> Director (Research Support) of Academic IT Services
> University of Oxford IT Services
> 13 Banbury Road, Oxford OX2 6NN. Phone +44 1865 283431



-- 
Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^