Reputation: 1271
Variables I have defined are coming through as strings, rather than propagating values.
Source
<?xml version="1.0"?>
<results>
<result>
<title_id>
123456
</title_id>
<product_id>
2
</product_id>
<track_id>
5
</track_id>
</result>
</results>
XSL
<html xsl:version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<body>
<table border="1" cellpadding="4" cellspacing="0">
<tr bgcolor="#fb0006" align="center">
<td><b>Link</b></td>
</tr>
<xsl:for-each select="//results/result">
<xsl:variable name="titlevar" select="title_id" />
<xsl:variable name="productvar" select="product_id" />
<xsl:variable name="trackvar" select="track_id" />
<tr>
<td><a href="{concat('http://www.myaddress/', '$titlevar', '-', '$productvar', '/?', '$trackvar')}" target="_blank">link</a></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
Outputs: http://www.myaddress/$titlevar-$productvar/?$trackvar
What I want is: http://www.myaddress/123456-5/?2
. I have tried all manner of brackets, apostrophes and speech marks. Can anyone see why it's not working?
Upvotes: 1
Views: 2219
Reputation: 50947
There should be no quote marks surrounding the variable references. Also note the use of normalize-space()
.
This works:
<html xsl:version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<body>
<table border="1" cellpadding="4" cellspacing="0">
<tr bgcolor="#fb0006" align="center">
<td><b>Link</b></td>
</tr>
<xsl:for-each select="//results/result">
<xsl:variable name="titlevar" select="normalize-space(title_id)" />
<xsl:variable name="productvar" select="normalize-space(product_id)" />
<xsl:variable name="trackvar" select="normalize-space(track_id)" />
<tr>
<td><a href="{concat('http://www.myaddress/',
$titlevar, '-', $productvar, '/?', $trackvar)}"
target="_blank">link</a></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
Upvotes: 2
Reputation: 101652
You need to remove the quotes around the variables:
<tr>
<td><a href="{concat('http://www.myaddress/', $titlevar,
'-', $productvar, '/?', $trackvar)}"
target="_blank">link</a></td>
</tr>
Upvotes: 0