1

Given:

<illustratedPartsCatalog>
    <figure id="fig1">...</figure>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
    <figure id="fig2">...</figure>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
    <catalogSeqNumber>...</catalogSeqNumber>
</illustratedPartsCatalog>

Each figure gets its own table of <catalogSeqNumber>s but right now the figure1 table also includes entries for figure2 and vice versa. The processing of <catalogSeqNumber> should stop when it reaches the next figure.

Solved with Tomalak's answer:

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

I added this to the end of <xsl:template match="figure">

<xsl:if test="following-sibling::*[1][self::catalogSeqNumber] and ancestor::illustratedPartsCatalog">
    <xsl:call-template name="PI-TABLE"/>
</xsl:if>

And added this to PI-TABLE (which builds the table of <catalogSeqNumber>):

<xsl:apply-templates select="key('kCSN', @id)" />
Caroline
  • 167
  • 1
  • 11

1 Answers1

0

<xsl:key> can help you with that.

<xsl:key name="kCSN" match="catalogSeqNumber" use="preceding-sibling::figure[1]/@id">

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

<xsl:template match="figure">
  <container>
    <xsl:apply-templates select="key('kCSN', @id)" />
  </container>
</xsl:template>

<xsl:template match="catalogSeqNumber">
  <!-- catalogSeqNumber processing ... -->
</xsl:template>

Here the key indexes all <catalogSeqNumber> elements by the @id of the immediateley preceding <figure>.

Now when we only process the <figure> elements, we can pull out the associated <catalogSeqNumber> elements easily with the key() function.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Brilliant, thank you, Tomalak. I saw `` used in a similar question but I couldn't figure out how to modify it. – Caroline Nov 30 '16 at 20:25
  • There is an explanation of xsl:key I wrote earlier, maybe it helps you wrapping your head around it. http://stackoverflow.com/questions/948218/xslt-3-level-grouping-on-attributes/955527#955527 – Tomalak Nov 30 '16 at 21:11
  • Danke, Tomalak. – Caroline Dec 04 '16 at 05:38