1

I am facing a problem trying to print one line only for a set of identical nodes and I could not find a similar post for identical nodes.

I have an input xml file containing a set of identical nodes, for instance it looks like:

<BookDetails>
<BK ISBN="123362367127" Shelf="Y" />
<BK ISBN="123362367127" Shelf="Y" />
<BK ISBN="123362367127" Shelf="Y" />
<BK ISBN="123362367127" Shelf="Y" />
</BookDetails>

What I want, is to print this book's information only once. I am thinking that a solution to my problem could be muenchian grouping, for instance group by ISBN value and then from the group print only the first one.

Based on that my code looks like that:

<xsl:key name="UniqueBKs" match="BK" use="@ISBN"/> 

<xsl:template name="BookDetails">
  <fo:table width="160mm" table-layout="fixed">
    <fo:table-column column-width="80mm" column-number="1"/>
    <fo:table-column column-width="80mm" column-number="2"/>
        <fo:table-body>
            <xsl:for-each select="BK[generate-id() = generate-id(key('UniqueBKs', @ISBN)[1])]">      
                <fo:table-row>
                    <xsl:apply-templates select="."/>
                </fo:table-row>  
            </xsl:for-each>
        </fo:table-body>
   </fo:table>
</xsl:template>

and:

<xsl:template match="BK">
    <fo:table-cell>
        <fo:block font-family="arial" font-size="8pt" text-align="left">
            <xsl:text>ISBN:</xsl:text>
        </fo:block>        
    </fo:table-cell>
    <fo:table-cell>
        <fo:block font-family="arial" font-size="8pt" text-align="right">
            <xsl:value-of select="@ISBN"/> 
        </fo:block>
    </fo:table-cell>
</xsl:template>

if my input xml file consists only from one BK element the code works fine. If I have more than one as shown above, Apache FOP returns me an error related with table-cells: "The column-number or number of cells in the row overflows the number of fo:table-columns specified for the table."

The error indicates that I am trying to put more table cells in my table row, while: a) I would expect only 1 row to be printed for this example b) more rows to be generated in case of more ISBNs, not more table cells.

Any help would be appreciated, thanks!

jeevium
  • 733
  • 6
  • 10
  • How does the generated XML look like? Does it feature exactly two cells per row in your table? – Tomalak Apr 23 '14 at 18:21
  • you mean the table-row I am trying to generate?? I am trying to generate a pdf, but right now it features exactly 2 cells per row yes. – jeevium Apr 23 '14 at 18:29
  • Hm, then it seems more of a FOP issue than an XSLT issue. At least from my point of view your XSLT is looking good; there shouldn't be more than two cells per row for that table. – Tomalak Apr 23 '14 at 18:42
  • Have you tried using a mode for `BK` template? You might have another apply-templates that is matching `BK` which is creating extra columns. – Matthew Green Apr 23 '14 at 21:48
  • Actually this exactly was my problem Matthew, I also discovered it some time ago. I was using the same template with a different mode for odd and even pages. But because my pdf document was only 1 page I thought that FOP validation might not care. I was wrong. Thanks for your time and answers both of you. – jeevium Apr 23 '14 at 21:56

2 Answers2

0
    <xsl:for-each select="BK[1]">
        <xsl:value-of select".">
    </xsl:for-each>

Adding [1] to the end of the for-each select you using will result in just the first match being "printed".

Eric Nord
  • 4,674
  • 3
  • 28
  • 56
0

is that really

<xsl:template name="BookDetails">

I think it should be

<xsl:template match="BookDetails">

or

<xsl:template name="BookDetails" match="BookDetails">
Joel M. Lamsen
  • 7,143
  • 1
  • 12
  • 14