xsl-list
[Top] [All Lists]

Re: [xsl] Aligning Parallel Columns

2011-01-15 09:51:39
At 2011-01-15 16:32 +0100, Jeroen Hellingman wrote:
Thanks for the hint. My current solution depends on being able to sort
the names, which isn't entirely satisfying ($a and $b contain the two
divs to be aligned):

Not sure why you have a problem with the sorting. The processor is going to create the groups based on the input sequences. That means likely that the leading sequence will dictate the order of the groups, which may present a problem for those groups populated only by the following sequence. Using the sort ensures the resulting order of the groups is correct.

    <table>
        <xsl:for-each-group group-by="@name" select="$a/p, $b/p">
            <xsl:sort select="@name"/>

            <xsl:variable name="name" select="@name"/>

            <tr>
                <td>
                    <xsl:value-of select="$name"/>
                </td>
                <td>
                    <xsl:if test="$a/p[@name = $name]">
                        <xsl:apply-templates select="$a/p[@name = $name]"/>
                    </xsl:if>
                </td>
                <td>
                    <xsl:if test="$b/p[@name = $name]">
                        <xsl:apply-templates select="$b/p[@name = $name]"/>
                    </xsl:if>
                </td>
             </tr>
        </xsl:for-each-group>
    </table>

You really haven't used <xsl:for-each-group> as intended. You could have achieved the above simply with the distinct-values() function, and would likely do so with XQuery 1.0 because of the lack of grouping. When creating groups, it is more conventional to actually access the groups you've just created rather than revisit the original source all over again. The above solution is quite wasteful by not taking advantage of the groups you make.

I hope the solution below helps to understand the direction one typically takes when creating groups. For completeness I've included the current-grouping-key() function, though in this case one could have simply used "@name" to get the same result. I tell my students the function is useful when you don't want to go through a lengthy recalculation of what was the basis of the creation of the current group.

. . . . . . . . . . Ken


t:\ftemp>type jeroen1.xml
        <div id="ch1">
            <p name="a1">Eerste Alinea.</p>
            <p name="a1.1">Zomaar ertussen.</p>
            <p name="a2">Tweede Alinea.</p>
            <p name="a3">Derde Alinea.</p>
        </div>

t:\ftemp>type jeroen2.xml
        <div id="ch2">
            <p name="a1">First Paragraph.</p>
            <p name="a2">Second Paragraph.</p>
            <p name="a2.1">Something added here.</p>
            <p name="a3">Third Paragraph.</p>
        </div>

t:\ftemp>xslt2 jeroen.xsl jeroen.xsl
<?xml version="1.0" encoding="UTF-8"?>
<table><!--a1--><tr>
      <td>Eerste Alinea.</td>
      <td>First Paragraph.</td>
   </tr>
   <!--a1.1--><tr>
      <td>Zomaar ertussen.</td>
      <td/>
   </tr>
   <!--a2--><tr>
      <td>Tweede Alinea.</td>
      <td>Second Paragraph.</td>
   </tr>
   <!--a2.1--><tr>
      <td/>
      <td>Something added here.</td>
   </tr>
   <!--a3--><tr>
      <td>Derde Alinea.</td>
      <td>Third Paragraph.</td>
   </tr>
</table>
t:\ftemp>type jeroen.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
  <xsl:variable name="parent1" select="document('jeroen1.xml')/*"/>
  <xsl:variable name="parent2" select="document('jeroen2.xml')/*"/>
  <table>
    <xsl:for-each-group select="$parent1/p,$parent2/p" group-by="@name">
      <xsl:sort select="@name"/>
      <xsl:comment select="current-grouping-key()"/>
      <tr>
        <td>
          <xsl:apply-templates select="current-group()[.. is $parent1]"/>
        </td>
        <td>
          <xsl:apply-templates select="current-group()[.. is $parent2]"/>
        </td>
      </tr>
    </xsl:for-each-group>
  </table>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>


--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>