Search code examples
xslthref

How to print the href with xslt?


I am a begginer in xml/html. I am trying to solve a little transformation from xml to html using xslt. I have this xml:

<?xml version="1.0" encoding="UTF-8"?>
<teams>
  <team cod="a101">
    <web>https://web1.com</web></team>
  <team cod="a102">
    <web>https://web2.com</web></team>
  <team cod="a103">
    <web>https://web3.com</web></team>
  <team cod="a104">
   <web>https://web4.com</web></team>
</teams>

I am trying to get this html result using xslt:

   Cod    Web
   a101  Show more
   a102  Show more
   a103  Show more
   a104  Show more

Where each "show more" links to each web.

I tried this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
        <xsl:template match="/"> 
            <xsl:apply-templates select="team"/>
            <html>
                <head>
                    <title>teams</title>                          
                    <meta charset="UTF-8"/>
                </head>
                <body>
                    <table>
                        <tr>
                            <td>Cod</td>
                            <td>Web</td>
                        </tr>                         
                            <xsl:for-each select="teams/team">
                        <tr>      
                       <td><xsl:value-of select="@cod"/></td>
                       <td><a href="I_don't_know_how_to_code_this">Show more</a></td>
                        </tr>   
                            </xsl:for-each>                     
                    </table>
                </body>
            </html>
        </xsl:template>                  
</xsl:stylesheet>

Anyone can help me, please?


Solution

  • The easiest way is to use an attribute value template:

    <td><a href="{web}">Show more</a></td>
                       
    

    You could also use xsl:attribute, but AVT is easier and reads nicer

    <td><a><xsl:attribute name="href" select="web"/>Show more</a></td>