开发者

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

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"/>
   开发者_Python百科</xsl:attribute>
   <xsl:text><xsl:value-of select="actionUrl"/></xsl:text> 
</xsl:element>


<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>


You can also do:

<a href="{actionUrl}"><xsl:value-of select="actionUrl"/></a>


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜