[xsl] Reshuffling elements

Subject: [xsl] Reshuffling elements
From: Morten Ryg <mort-ry@xxxxxxxxx>
Date: Tue, 26 Oct 2004 13:32:15 +0200
Exporting records from a Filemaker file, I get an XML file with the following structure for each record:

    <row modid="147" recordid="20">
      <col> <!-- Category -->
        <data>
          Medicine
        </data>
      </col>
      <col> <!-- Journal name -->
        <data>
          British Medical Journal
        </data>
      </col>
      <col> <!-- URL -->
        <data>
          http://bmj.bmjjournals.com/
        </data>
      </col>
      <col>
        <data>
          Fulltekstartikler tilgjengelig ved publikasjon
        </data>
      </col>
      <col> <!-- col 5: Auxiliary categories -->
        <data>
          Publisher
        </data>
        <data>
          Organisation
        </data>
      </col>
      <col> <!-- col 6: Name of auxiliary link -->
        <data>
          BMJ Publishing Group
        </data>
        <data>
          British Medical Association
        </data>
      </col>
      <col> <!-- col 7: Auxiliary URL -->
        <data>
          http://www.bmjpg.com/
        </data>
        <data>
          http://www.bma.org.uk/
        </data>
      </col>
    </row>
    
 I want to transform this into HTML in the form

 Journal title
 Link category: Link name name, URL 
 
 There can be any number of <data> elements per <col> element for col no 5 - 7, but the same number for each of these columns.

The following code produces the output I am after for data element no 1:

    <xsl:if test="./fmp:COL[6]/fmp:DATA[1] != '' ">
      <p>
        <em>
          <xsl:value-of select="fmp:COL[5]/fmp:DATA[1]" />
          <xsl:text>: </xsl:text> 
        </em>
          <xsl:value-of select="fmp:COL[6]/fmp:DATA[1]" />
          <xsl:text>, URL: </xsl:text>
          <xsl:value-of select="fmp:COL[7]/fmp:DATA[1]" />
      </p>
    </xsl:if>


British Medical Journal 
Organisation: British Medical Association, URL: http://www.bma.org.uk/
Publisher: BMJ Publishing Group, URL: http://www.bmjpg.com/


I can extend this to heart's content, by adding more blocks, and increasing the DATA[1] to DATA[2], DATA[3] and so on, but I would rather have solution for any number of auxiliary links. When I try the following;

    <xsl:for-each select="fmp:COL[6]/fmp:DATA">
      <xsl:param name="n_pos" select="position()" />
        
        <p>
        <xsl:value-of select="$n_pos" />. 
         <xsl:value-of select="." />, 
        <xsl:value-of select="../following-sibling/child[1]" /> 
        </p>
    </xsl:for-each>
    
I get the result:

British Medical Journal 

1. British Medical Association,
2. BMJ Publishing Group,

in other words, the data elemenents of col[7] are left out. 

If I modify the following-sibling line, using 
<xsl:value-of select="../following-sibling::*" />
 instead of 
 <xsl:value-of select="../following-sibling/child[1]" />,
 
I get a listing of the URLs of all auxiliary links after each auxiliary link name, as expected: 

British Medical Journal 
1. British Medical Association, http://www.bma.org.uk/http://www.bmjpg.com/
2. BMJ Publishing Group, http://www.bma.org.uk/http://www.bmjpg.com/

How can I get this right? 
-- 
***************************
Morten Ryg
Glittreklinikken
1488 Hakadal
Norway
tel: 	  +47 67058283
mobil:	      938 53 076
fax:	  +47 67075344
mail:     mort-ry@xxxxxxxxx
web       http://home.online.no/~mort-ry/
***************************

Current Thread