RE: [xsl] "grouping" footnote numbers

Subject: RE: [xsl] "grouping" footnote numbers
From: Emma Burrows <Emma.Burrows@xxxxxxxxxxx>
Date: Tue, 11 Oct 2011 14:33:15 +0100
Thanks, Brandon,

count="xref[not(@href = preceding::xref/@href)]"

works perfectly when combined with the key filtering. I see Murray suggests
something more efficient, but one of the things I've learned recently is how
to avoid the "@x = preceding::*/@x" pattern by doing this:

xref[count(.|key('fn',@href)[1])=1]

Since I already had the key, might as well make use of it, though the
documentlets in my CMS are so small it's hard to tell if it makes any
difference!

So that's what I've got now:

<xsl:key name="fn" match="xref" use="@href"/>
...
<xsl:template match="xref">
  <a href="{@href}">
    <sup>
      <xsl:for-each-group select="key('fn', @href)" group-by="@href">
        <xsl:number level="any" count="xref[count(.|key('fn',@href)[1])=1]"/>
      </xsl:for-each-group>
    </sup>
  </a>
</xsl:template>

I hope my users will be impressed (but since they view most of what we do as
magic anyway, they probably won't be). Thank you all for your help.


-----Original Message-----
From: Brandon Ibach [mailto:brandon.ibach@xxxxxxxxxxxxxxxxxxx]
Sent: 11 October 2011 14:08
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] "grouping" footnote numbers

By calling key() from the context of the xref, you're only getting
back the xrefs with the same @href, so you'll only ever have one group
and position() will only ever return 1.

It may not be terribly efficient, but you could add this to the
<xsl:number> in the XSLT code you posted:

count="xref[not(@href = preceding::xref/@href)]"

Or, replace the <xsl:number> completely with:

<xsl:value-of select="count(distinct-values(preceding::xref/@href)) + 1"/>

Determining which one works better is left as an exercise for the OP. :)

-Brandon :)


On Tue, Oct 11, 2011 at 8:42 AM, Emma Burrows <Emma.Burrows@xxxxxxxxxxx>
wrote:
> From: David Carlisle [mailto:davidc@xxxxxxxxx]
>> change that to xsl:value-of select="position()"  having grouped them all
>> you just want to know which group you ar in and position(0 tells you that
>
> Thank you very much for looking into this. Alas, this XSL:
>
> <xsl:key name="fn" match="xref" use="@href"/>
> ...
> <sup>
>  <xsl:for-each-group select="key('fn', @href)" group-by="@href">
>    <xsl:value-of select="position()"/>
>  </xsl:for-each-group>
> </sup>
>
> Just results in every reference being numbered as 1. I'm not sure why -
either because the grouping is based on a key or because this appears in the
"xref" template?
>
> To move this forward, here is some actual XML for you:
>
> Input
> =====
>    <p><b>Caesarean section</b></p>
>    <p>Single dose<xref href="footnote-113022.dita#bnf_113022" type="fn"/> of
i/v cefuroxime</p>
>    <p>Administer immediately after umbilical cord is clamped. Add i/v
teicoplanin<xref href="footnote-202621.dita#bnf_202621" type="fn"/></p>
>    <p><b>Hysterectomy</b><xref href="footnote-204127.dita#bnf_204127"
type="fn"/></p>
>    <p>Single dose<xref href="footnote-113022.dita#bnf_113022" type="fn"/> of
i/v cefuroxime + i/v
>        metronidazole<xref href="footnote-113019.dita#bnf_113019"
type="fn"/>
>      <i>or</i> i/v gentamicin + i/v metronidazole<xref
href="footnote-113019.dita#bnf_113019" type="fn"/>
>      <i>or</i> i/v co-amoxiclav alone</p>
>    <p>Use single dose<xref href="footnote-113022.dita#bnf_113022"
type="fn"/> of
>        i/v gentamicin + i/v metronidazole<xref
href="footnote-113019.dita#bnf_113019" type="fn"/> or
>        add i/v teicoplanin<xref href="footnote-202621.dita#bnf_202621"
type="fn"/></p>
>    <p><b>Termination of pregnancy</b></p>
>    <p>Single dose<xref href="footnote-113022.dita#bnf_113022" type="fn"/> of
oral metronidazole</p>
>
> XSL:
> ====
>
>  <xsl:key name="fn" match="xref[@type='fn']" use="@href"/>
>
>  <xsl:template match="p">
>    <xsl:element name="{local-name()}"><xsl:apply-templates/></xsl:element>
>  </xsl:template>
>
>  <xsl:template match="xref">
>    <a href="{@href}">
>      <sup>
>        <xsl:for-each-group select="key('fn', @href)" group-by="@href">
>          <xsl:number level="any"/>
>        </xsl:for-each-group>
>      </sup>
>    </a>
>  </xsl:template>
>
> (Incorrect) Output:
> ===================
>
>  <p>Caesarean section</p>
>  <p>Single dose<a href="footnote-113022.dita#bnf_113022"><sup>1</sup></a> of
i/v cefuroxime</p>
>  <p>Administer immediately after umbilical cord is clamped. Add i/v
teicoplanin<a href="footnote-202621.dita#bnf_202621"><sup>2</sup></a></p>
>  <p>Hysterectomy<a
href="footnote-204127.dita#bnf_204127"><sup>3</sup></a></p>
>  <p>Single dose<a href="footnote-113022.dita#bnf_113022"><sup>1</sup></a> of
i/v cefuroxime + i/v
>    metronidazole<a href="footnote-113019.dita#bnf_113019"><sup>5</sup></a>
>    or i/v gentamicin + i/v metronidazole<a
href="footnote-113019.dita#bnf_113019"><sup>5</sup></a>
>    or i/v co-amoxiclav alone</p>
>  <p>Use single dose<a
href="footnote-113022.dita#bnf_113022"><sup>1</sup></a> of
>    i/v gentamicin + i/v metronidazole<a
href="footnote-113019.dita#bnf_113019"><sup>5</sup></a> or
>    add i/v teicoplanin<a
href="footnote-202621.dita#bnf_202621"><sup>2</sup></a></p>
>  <p>Termination of pregnancy</p>
>  <p>Single dose<a href="footnote-113022.dita#bnf_113022"><sup>1</sup></a> of
oral metronidazole</p>
>
> So essentially, I get footnote reference numbers 1, 2, 3 and 5. I'd like
number 5 to come out as number 4 as I'd expect. Thank again, David, and
whoever would like to jump in to give you a break. :)


______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________

Current Thread