Setting xsl:value-of into an href attribute and the text field of a link in an XSLT
Asked Answered
B

4

42

How can I set an a href that is both a link to and has the text for a link through an XSLT transformation? Here's what I have so far, which gives me the error "xsl:value-of cannot be a child of the xsl:text element":

<xsl:element name="a">
   <xsl:attribute name="href">
      <xsl:value-of select="actionUrl"/>
   </xsl:attribute>
   <xsl:text><xsl:value-of select="actionUrl"/></xsl:text> 
</xsl:element>
Baronet answered 1/4, 2010 at 19:19 Comment(0)
E
51

<xsl:text> defines a text section in an XSL document. Only real, plain text can go here, and not XML nodes. You only need <xsl:value-of select="actionUrl"/>, which will print text anyways.

<xsl:element name="a">
    <xsl:attribute name="href">
        <xsl:value-of select="actionUrl"/>
    </xsl:attribute>
    <xsl:value-of select="actionUrl"/>
</xsl:element>
Expressly answered 1/4, 2010 at 19:21 Comment(2)
Just a note, it seems important to nominate the href first, and display text second as you have done. I had it the other way around it it failed to display.Cootch
This is bulky and hard to read, check out lexicore's answer!Innate
P
31

You can also do:

<a href="{actionUrl}"><xsl:value-of select="actionUrl"/></a>
Pentastich answered 1/4, 2010 at 19:25 Comment(1)
Actually, actionurl is not a variable -- just an element name. Therefore, thete must be no "$" sign at the start of the name.Ronel
G
6

You don't need the xsl:text element:

<xsl:element name="a">
  <xsl:attribute name="href">
    <xsl:value-of select="actionUrl"/>
  </xsl:attribute>
  <xsl:value-of select="actionUrl"/>
</xsl:element>
Gerson answered 1/4, 2010 at 19:21 Comment(0)
P
0

I wanted to use a .xsl to guarantee consistency of hyperlinks across a number of XML extracts being formatted as .html reports. Each record has a primary key called ID - an automatically incrementing number - which is passed as a parameter to various reports, but never shown as a column in those reports. Here's how I did it.

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <html>
        <body>
            <table>
                <xsl:for-each select="table/row">
                    <tr>
                        <xsl:apply-templates>
                            <!-- id is primary key and is passed as a parameter to all the templates whether they need it or not -->
                            <xsl:with-param name="id"><xsl:value-of select="id"/></xsl:with-param>
                        </xsl:apply-templates>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

<xsl:template match="id">
    <!-- id is primary key and is never shown -->
</xsl:template>

<xsl:template match="employee_number">
    <!-- employee_number field always links to the attendance report -->
    <xsl:param name="id"/>
    <xsl:variable name="name"><xsl:value-of select="name(.)"/></xsl:variable>
    <td id="{$name}"><a href="attendance?id={$id}"><xsl:value-of select="."/></a></td>
</xsl:template>

<!-- other templates redacted for clarity/brevity -->

<xsl:template match="*">
    <!-- any field without a dedicated template is just a cell in the table -->
    <xsl:variable name="name" select="name(.)"/>
    <td id="{$name}"><xsl:value-of select="."/></td>
</xsl:template>

</xsl:stylesheet>
Pelag answered 12/4, 2022 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.