Q:

Generic XSLT that transforms XML into HTML table with rules

So I want to make an XSLT that would transform any XML following the rule:
<root1>
   <elem1>text</elem1>
   <elem2>text</elem2>
   ...
   <elemx>text</elemx>
   ... 
   <image>someimgreference</image
   <linkref>alink</linkref>
</root1>

<root2>
   <elem1>text</elem1>
   <elem2>text</elem2>
   ...
   <elemx>text</elemx>
   ... 
   <image>someimgreference</image
   <linkref>alink</linkref>
</root2>

using the XSLT I would get a table with the titles and when I have text, I only show the text, when I have image link I get the actual image with
<xsl:template match="image">
    <img src="{@url}" />
</xsl:template>

or something
and when I have a linkref, I just post the click-able link
root1/root2 is just an exemple... those could be any names, same goes for elem1/2/3/... I want the xslt to work with any XML with that structure. So no element name references.
HTML table would look like:
    elem1 | elem2 |... |image|linkref
  ------------------------------------
    text  | text  |... |photo|link
    text  | text  |... |photo|link

I found some good references online but none really helped me get the table or none were generic enough. Is it possible what I want? Or how close can I get to getting this generic xslt?
This is the most generic I've found. I just need the image links to be shows as actual photos and link references to click-able links
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

<xsl:template match="/*">
<TABLE BORDER="1">
<TR>
        <xsl:for-each select="*[position() = 1]/*">
          <TD>
              <xsl:value-of select="local-name()"/>
          </TD>
        </xsl:for-each>
</TR>
      <xsl:apply-templates/>
</TABLE>
</xsl:template>

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

<xsl:template match="/*/*/*">
<TD>
    <xsl:value-of select="."/>
</TD>
</xsl:template>
</xsl:stylesheet>

I don't know where to insert the image template or the link templates (or how to write the link template) 
I tried inserting it like a normal template and it doesn't work. It won't do anything with the link. Not to mention the clickable one...
Any ideas? At all?

A:

You've got the right template to use in comments, however you probably need to add a priority attribute to it to ensure it gets matched before the one matching /*/*/*
<xsl:template match="image" priority="2">
   <td>
    <img src="{.}" /> 
   </td>
</xsl:template>

Alternatively, to save repeatedly outputting td tags for each template, you could output in the template that matches tr before applying the templates to match the child nodes
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">
<HTML>
<BODY>
    <xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="/*">
<TABLE BORDER="1">
<TR>
        <xsl:for-each select="*[position() = 1]/*">
          <TD>
              <xsl:value-of select="local-name()"/>
          </TD>
        </xsl:for-each>
</TR>
      <xsl:apply-templates/>
</TABLE>
</xsl:template>

<xsl:template match="/*/*">
<TR>
    <xsl:for-each select="*">
      <td>
        <xsl:apply-templates select="." />
      </td>
  </xsl:for-each>
</TR>
</xsl:template>

<xsl:template match="image">
   <img src="{.}" /> 
</xsl:template>

<xsl:template match="linkref">
  <a href="{.}">
    <xsl:value-of select="." />
  </a>
</xsl:template>
</xsl:stylesheet>

