K Green
K Green

Reputation: 129

Using a link in XSL Files

I am completely new to XML so hopefully my explanations will make sense..

I am using an XSL file with 2 sections of HTML text, reading from 2 templates set up within the file.

The first text and associated template is a table, and I have set up a link within that. I want the link to then point to a picture that is part of the 2nd HTML text/template.

The link in the table is setup in that it appears as a link, underlined, etc. However it doesnt go anywhere when clicked.

The second section works fine on it's own, eg, pictures and text appears.

But what I cant figure out is how to actually get the link to work. I've tried numerous things but nothing has worked so far. And I-m not sure whether I am very close and just perhaps need to alter a line of code. Or whether I need to be doing something very different. Also, there are no error messages, and everything displays well, it's just the link itself that doesnt work.

 <xsl:template match="portion">
  <tr>
 <td valign="top"><xsl:value-of select="food-description"/></td>
 <td valign="top"><xsl:value-of select="food-number"/></td>
 <!--the following is the link text-->  
 <td valign="top"><a><xsl:attribute name="href">#<xsl:value-of select="portion-     photo/@file"/></xsl:attribute>
 <xsl:value-of select="portion-photo/@file"/></a><br/>
 </td>
 </tr>
 </xsl:template>


 <xsl:template match="portion-photo">
 <!--I know that this is the code that is not correct, however, believe it should      be something similar-->  
 <a><xsl:attribute name="portion-photo"><xsl:value-of select="./@file"/></xsl:attribute></a>  

 <p><xsl:value-of select="../food-description"/>
 <xsl:value-of select="./@file"/></p>

 <img>
   <xsl:attribute name="src"><xsl:value-of select="./@file"/></xsl:attribute>
   <xsl:attribute name="width"><xsl:value-of select="ceiling(./@w div v2)"/></xsl:attribute>
   <xsl:attribute name="height"><xsl:value-of select="ceiling(./@h div 2)"/></xsl:attribute>
  </img>
 </xsl:template>

Upvotes: 1

Views: 2537

Answers (1)

Martin
Martin

Reputation: 1788

Something like the following should work. Just add the missing name attribute to the anchor element:

<xsl:template match="portion">
  ...
  <a href="#{portion-photo/@file}">
    <xsl:value-of select="portion-photo/@file"/>
  </a>
  ...
</xsl:template>

<xsl:template match="portion-photo">
  <a name="{@file}">
    <xsl:value-of select="@file"/>
  </a>
</xsl:template>

However, you have to ensure that @file evaluates to a valid anchor name. If the values of all file attributes are unique, you could also create save IDs with generate-id():

<xsl:template match="portion">
  ...
  <a href="#{generate-id(portion-photo)}">
    <xsl:value-of select="portion-photo/@file"/>
  </a>
  ...
</xsl:template>

<xsl:template match="portion-photo">
  <a name="{generate-id()}">
    <xsl:value-of select="@file"/>
  </a>
</xsl:template>

Upvotes: 1

Related Questions