0

Can anyone help? I am very new to XSLT, and am trying to build a table of elements; I've simplified my example down below, and have managed to get output onto rows of three cells, but I'm getting unwanted whitespace between the rows - could anyone tell me what I'm doing wrong here?

Also do I need two <apply-templates /> in my Rows Match?

Many thanks in advance, Alex

This is XML:

<?xml version="1.0" encoding="iso-8859-1"?>
    <products>
        <r t='title1'>...</r>
        <r t='title2'>...</r>
        <r t='title3'>...</r>
        <r t='title4'>...</r>
        <r t='title5'>...</r>
        <r t='title6'>...</r>
        <r t='title7'>...</r>
        <r t='title8'>...</r>
        <r t='title9'>...</r>
    </products>

This is XSL:

<!-- Rows -->
<xsl:template match="r[position() mod 3 = 1]">
    <div class="row">
        <xsl:apply-templates mode="cell" select="." />
        <xsl:apply-templates mode="cell" select="./following-sibling::r[not(position() > 2)]" />
    </div>
</xsl:template>

<!-- Cells -->
<xsl:template match="r" mode="cell">
    <div class="cell">
        <xsl:value-of select="@t"/>
    </div>
</xsl:template>

MY OUTPUT (Note the unwanted whitespace between rows):

<div class="row">
    <div class="cell">Title1</div>
    <div class="cell">Title2</div>
    <div class="cell">Title3</div>
</div>









<div class="row">
    <div class="cell">Title4</div>
    <div class="cell">Title5</div>
    <div class="cell">Title6</div>
</div>









<div class="row">
    <div class="cell">Title7</div>
    <div class="cell">Title8</div>
    <div class="cell">Title9</div>
</div>
Alex2134
  • 557
  • 7
  • 22

1 Answers1

0

Well, to start with that cannot be your XML as that is invalid. There is no way a "valid" XML file contains more that one root element. Your XML has nine "r" elements at the root.

Start with something valid first before attempting to process it with XSLT.

This XSL (note I added a template to eliminate the match for any text() because your "..." would be matched otherwise:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="r[position() mod 3 = 1]">
    <div class="row">
        <xsl:apply-templates mode="cell" select="." />
        <xsl:apply-templates mode="cell" select="./following-sibling::r[not(position() > 2)]" />
    </div>
</xsl:template>

<xsl:template match="r" mode="cell">
    <div class="cell">
        <xsl:value-of select="@t"/>
    </div>
</xsl:template>

<xsl:template match="text()"/>
</xsl:stylesheet>

Produces this output using Saxon (no new lines at all):

<?xml version="1.0" encoding="utf-8"?><div class="row"><div class="cell">title1</div><div class="cell">title2</div><div class="cell">title3</div></div><div class="row"><div class="cell">title4</div><div class="cell">title5</div><div class="cell">title6</div></div><div class="row"><div class="cell">title7</div><div class="cell">title8</div><div class="cell">title9</div></div>

If you add <xsl:output indent="yes"/> to this, you would get the exact output you expect. You would also get invalid XML output unless you specify a match for <products> to output the root tag.

Kevin Brown
  • 8,805
  • 2
  • 20
  • 38
  • Hi @KevinBrown apologies for that, fixed my XML example. – Alex2134 Jun 17 '13 at 12:21
  • I do not think that is "whitespace" as you are think of it. In your input XML what is "really" between the and the ? It cannot be "..." or other content unless you have a template that eliminates that match. Given the short example you provided, the "..." is still hit and would be in the output and in fact would be between each block of rows. – Kevin Brown Jun 18 '13 at 21:22
  • What do you have at the top of your XSL and other possible templates, one cannot diagnose with seeing it all. I suspect you have something else that is matching. – Kevin Brown Jun 18 '13 at 21:25
  • As an alternate approach I just wrote for a different question. You could easily adapt that answer to your questions also. It uses recursion instead of the way you are doing things here. See [this article](http://stackoverflow.com/questions/17179352/xslt-generate-dynamic-columns-for-apache-fop/17181854#17181854) – Kevin Brown Jun 19 '13 at 02:57