Numbering Nodes

Discussion in 'XML' started by Dwayne Wilkinson, May 7, 2004.

  1. Hi,

    I need to number a set of PARA nodes as outlined below:

    <?xml version="1.0" encoding="utf-8"?>
    <DESCRIPT>
    <PARA0>
    <PARA>Paragraph 1</PARA>
    </PARA0>
    <PARA0>
    <PARA>Paragraph 2</PARA>
    <SPECPARA>
    <CAUTION>
    <PARA>Special Caution paragraph 1</PARA>
    </CAUTION>
    <PARA>Paragraph 3</PARA>
    </SPECPARA>
    <PARA>Paragraph 4</PARA>
    </PARA0>
    <DESCRIPT>


    <!-- Desired Output:

    1. Paragraph 1

    2. Paragraph 2

    -- CAUTION --
    Special Caution paragraph 1

    3. Paragraph 3

    Paragraph 4


    I have created the following XSLT to do this, but it doesn't quite
    work the way I'd like:

    <!-- SNIP -->
    <xsl:template match="PARA">
    <xsl:choose>
    <xsl:when test="(name(../..) = 'PARA0' and name(..) = 'SPECPARA')
    or name(..) = 'PARA0'">
    <table style="width:100%;border:0;" class="para0">
    <tr style="vertical-align:top;">
    <td style="width:12mm;font-weight:bold;">
    <xsl:if test="self::node() [generate-id() =
    generate-id(parent::node()/child::pARA [position() = 1])]">
    <xsl:number format="1. "
    count="PARA0 [PARA != '' or SPECPARA/PARA = '']"
    level="single" />
    </xsl:if>
    </td>
    <td class="para">
    <a>
    <xsl:attribute name="name">
    <xsl:value-of select="@ID"/>
    </xsl:attribute>
    <xsl:apply-templates />
    </a>
    </td>
    </tr>
    </table>
    </xsl:when>
    <!-- SNIP -->

    This produces the following results:
    1. Paragraph 1

    2. Paragraph 2

    2. Paragraph 3

    Paragraph 4

    Any help on how to number Paragraph 3 as 3 instead of 2 would be
    greatly appreciated.

    Regards
    Dwayne
    Dwayne Wilkinson, May 7, 2004
    #1
    1. Advertising

  2. Hi,

    (Dwayne Wilkinson) writes:
    > I need to number a set of PARA nodes as outlined below:
    >
    > <?xml version="1.0" encoding="utf-8"?>
    > <DESCRIPT>
    > <PARA0>
    > <PARA>Paragraph 1</PARA>
    > </PARA0>
    > <PARA0>
    > <PARA>Paragraph 2</PARA>
    > <SPECPARA>
    > <CAUTION>
    > <PARA>Special Caution paragraph 1</PARA>
    > </CAUTION>
    > <PARA>Paragraph 3</PARA>
    > </SPECPARA>
    > <PARA>Paragraph 4</PARA>
    > </PARA0>
    > <DESCRIPT>
    >
    >
    > <!-- Desired Output:
    >
    > 1. Paragraph 1
    >
    > 2. Paragraph 2
    >
    > -- CAUTION --
    > Special Caution paragraph 1
    >
    > 3. Paragraph 3
    >
    > Paragraph 4


    If you change your xsl:number element to the following it will do the
    job, if I understand correctly (note use of value attribute rather
    than count).

    <xsl:number format="1. "
    value="1+count(preceding::pARA[not(name(../..)='SPECPARA')])"
    level="single" />

    It does seem strange markup though - let's see if I understand in
    words what you want:

    You want to output PARA elements that are the children of PARA0 or
    the children of SPECPARA and grandchildren of PARA0.

    You also want to handle the SPECPARA/CAUTION/PARA element
    separately.

    You want to number consecutively PARA elements that are the first
    PARA children of their parents whether that parent is PARA0 or
    SPECPARA


    If I've understood correctly the following should help. I've
    re-organised it what you might call a more XSLT way which simplifies
    some of the logic, and easily allows you to handle the CAUTION case.


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

    <xsl:eek:utput indent="yes"/>

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

    <xsl:template match="PARA0">
    <xsl:apply-templates select="PARA | SPECPARA"/>
    </xsl:template>

    <xsl:template match="SPECPARA">
    <h2>Caution!!!</h2>
    <xsl:value-of select="CAUTION/PARA"/>
    <xsl:apply-templates select="PARA"/>
    </xsl:template>

    <xsl:template match="PARA">
    <table style="width:100%;border:0;" class="para0">
    <tr style="vertical-align:top;">
    <td style="width:12mm;font-weight:bold;">
    <xsl:if test="position() = 1">
    <xsl:number format="1. "
    value="1+count(preceding::pARA[not(name(../..)='SPECPARA')])"
    level="single" />
    </xsl:if>
    </td>
    <td class="para">
    <a>
    <xsl:attribute name="name">
    <xsl:value-of select="@ID"/>
    </xsl:attribute>
    <xsl:apply-templates />
    </a>
    </td>
    </tr>
    </table>
    </xsl:template>

    </xsl:stylesheet>


    --
    Ben Edgington
    Mail to the address above is discarded.
    Mail to ben at that address might be read.
    http://www.edginet.org/
    Ben Edgington, May 7, 2004
    #2
    1. Advertising

  3. Ben Edgington <> wrote in message news:<>...
    >
    > If you change your xsl:number element to the following it will do the
    > job, if I understand correctly (note use of value attribute rather
    > than count).
    >
    > <xsl:number format="1. "
    > value="1+count(preceding::pARA[not(name(../..)='SPECPARA')])"
    > level="single" />
    >
    > It does seem strange markup though - let's see if I understand in
    > words what you want:
    >
    > You want to output PARA elements that are the children of PARA0 or
    > the children of SPECPARA and grandchildren of PARA0.
    >
    > You also want to handle the SPECPARA/CAUTION/PARA element
    > separately.
    >
    > You want to number consecutively PARA elements that are the first
    > PARA children of their parents whether that parent is PARA0 or
    > SPECPARA
    >
    >
    > If I've understood correctly the following should help. I've
    > re-organised it what you might call a more XSLT way which simplifies
    > some of the logic, and easily allows you to handle the CAUTION case.
    >
    >
    > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:eek:utput indent="yes"/>
    >
    > <xsl:template match="/">
    > <xsl:apply-templates select="//PARA0"/>
    > </xsl:template>
    >
    > <xsl:template match="PARA0">
    > <xsl:apply-templates select="PARA | SPECPARA"/>
    > </xsl:template>
    >
    > <xsl:template match="SPECPARA">
    > <h2>Caution!!!</h2>
    > <xsl:value-of select="CAUTION/PARA"/>
    > <xsl:apply-templates select="PARA"/>
    > </xsl:template>
    >
    > <xsl:template match="PARA">
    > <table style="width:100%;border:0;" class="para0">
    > <tr style="vertical-align:top;">
    > <td style="width:12mm;font-weight:bold;">
    > <xsl:if test="position() = 1">
    > <xsl:number format="1. "
    > value="1+count(preceding::pARA[not(name(../..)='SPECPARA')])"
    > level="single" />
    > </xsl:if>
    > </td>
    > <td class="para">
    > <a>
    > <xsl:attribute name="name">
    > <xsl:value-of select="@ID"/>
    > </xsl:attribute>
    > <xsl:apply-templates />
    > </a>
    > </td>
    > </tr>
    > </table>
    > </xsl:template>
    >
    > </xsl:stylesheet>


    Hi Ben,

    Thanks for the reply, yes it sounds like you understood the problem
    correctly, the XSLT is meant to create online technical manuals from a
    couple of different Defence SGML standards (converted to XML).

    I'll give it a try tomorrow to see how it goes.

    Thanks for the excellent and detailed response.

    Regards
    Dwayne
    Dwayne Wilkinson, May 9, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. asd
    Replies:
    3
    Views:
    435
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    511
    gavnosis
    Aug 2, 2003
  3. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    401
    Patrick TJ McPhee
    Jun 17, 2004
  4. Johnny Ooi

    Looking A Nodes From Within Nodes

    Johnny Ooi, Nov 13, 2004, in forum: XML
    Replies:
    10
    Views:
    652
    Johnny Ooi
    Nov 14, 2004
  5. Replies:
    2
    Views:
    390
Loading...

Share This Page