[xsl] Numbering selected output sequentially

Subject: [xsl] Numbering selected output sequentially
From: "Mark Wilson" <drmark@xxxxxxxxxxxxxxx>
Date: Fri, 8 Sep 2006 10:41:27 -0400
Problem:

I am selecting items from the <Plate> document below based upon there being content in the <BottomPane> element. In the Output <List> XML document, I want to number each sequentially in an <Instance> element. That is, if I find 10 instances of <BottomPanel> with data, I want the <Instance> elemets to read 1 through 10. Not all <Stamp> elements have data in the <BottomPanel> element, I am trying to select, output, and count those that do. <xsl:number> does not work the way I am using it. Can you help? (I have omitted most of the 100 <Stamp> elements in the <Plate> document and foreshortened the stylesheet to just the main template. I hope that was OK. I have typed the XML/XSLT in by hand and hope there are no errors. I have looked in the XSLT Cookbook but clearly did not understand <xsl:number>

Mark
--------------

OUTPUT
<List>
<Entry>
<Instance>1</Instance>
<BottomPanel>Dark spot in the white area above the numeral 1</BottomPanel>
<Location>
<Design>II</Design>
<Value>100</Value>
<PlateNumber>1</PlateNumber>
<Position>1</Position>
</Location>
</Entry>
<Entry>
<Instance>2</Instance>
<BottomPanel>Line across the throat and breast of the left dove</BottomPanel>
<Location>
<Design>II</Design>
<Value>100</Value>
<PlateNumber>1</PlateNumber>
<Position>100</Position>
</Location>
</Entry>


STYLESHEET TEMPLATE
<xsl:template match="Stamp/BottomPanel">
     <xsl:if test="string-length(.)">
      <Entry>
       <Instance>
            <xsl:number count="*" format="1" /> <!-- DOES NOT WORK -->
       </Instance>
       <xsl:copy-of select="." />
       <Location>
            <xsl:copy-of select="../../Design" />
            <xsl:copy-of select="../../Value" />
            <xsl:copy-of select="../../PlateNumber" />
            <xsl:copy-of select="../Position"></xsl:copy-of>
      </Location>
    </Entry>
 </xsl:if>
 </xsl:template>

DOCUMENT
I have an xml document which has roughly the below structure but with 100 "stamp" objects:
<?xml version="1.0" encoding="UTF-8"?>
<Plate>
<Design>II</Design>
<Value>100</Value>
<PlateNumber>1</PlateNumber>
<Stamp>
<Position>1</Position>
<TopPanel></TopPanel>
<LeftPanel>[Notch in the left edge opposite the C]</LeftPanel>
<RightPanel></RightPanel>
<CenterPanel>Dot over the sun at 12:15</CenterPanel>
<BottomPanel>Dark spot in the white area above the numeral 1</BottomPanel>
</Stamp>
<Stamp>
<Position>2</Position>
<TopPanel></TopPanel>
<LeftPanel>[Notch in the left edge opposite the C]</LeftPanel>
<RightPanel></RightPanel>
<CenterPanel>Dot over the sun at 12:15</CenterPanel>
<BottomPanel></BottomPanel>
</Stamp>
<Stamp>
<Position>100</Position>
<TopPanel>Dot in the top panel under P</TopPanel>
<LeftPanel></LeftPanel>
<RightPanel></RightPanel>
<CenterPanel>dot to the left of the tip of the third tower; dot on the sun at 2:00</CenterPanel>
<BottomPanel>Line across the throat and breast of the left dove</BottomPanel>
</Stamp>
</Plate>


Current Thread