Numbering Nodes

  • Thread starter Dwayne Wilkinson
  • Start date
D

Dwayne Wilkinson

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
 
B

Ben Edgington

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

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

Dwayne Wilkinson

Ben Edgington said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top