Can't figure out the correct parameters for this xsl:key example to work. What I want is to output the same xhtml with rate/name fields changed based on the data in root.xml.
XHMTL (main input)
<!DOCTYPE html>
<html lang="en">
<head>
<title>PZBank</title>
</head>
<body>
<table style="width:100%">
<tr>
<th align="left">Product #1</th>
<th align="left">Product #2</th>
<th align="left">Product #3</th>
</tr>
<tr>
<td name="name0">Whiz-bang</td>
<td name="name1">Ulitmate</td>
<td name="name2">Killer</td>
</tr>
<tr>
<td name="rate0">2.09</td>
<td name="rate1">1.99</td>
<td name="rate2">3.19</td>
</tr>
</table>
</body>
</html>
XML - root.xml
<root>
<dbu>
<product ord="0">
<name>Amazing</name>
<rate>4.5</rate>
</product>
</dbu>
<dbu>
<product ord="1">
<name>Incredible</name>
<rate>6.6</rate>
</product>
</dbu>
</root>
XSL
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" include-content-type="no"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:variable name="rooter" select="document('root.xml')"/>
<xsl:key name="krate" match="product/rate" use="/root/dbu/product[@ord]"/>
<xsl:key name="kname" match="name" use="@ord"/>
<xsl:template match="td[starts-with(@name,'rate')]/text()">
<xsl:variable name="ordv" select="substring-after(../@name,'rate')"/>
<xsl:for-each select="$rooter">
<xsl:value-of select="key('krate',$ordv)"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="td[starts-with(@name,'name')]/text()">
{key('kname', substring-after(../@name, 'name'), $rooter)}
</xsl:template>
</xsl:stylesheet>
What is happening is the name/rate fields are blank in the output so obviously the xsl:key calls are incorrect. I have even tried 2 approaches: krate and kname but no luck. I have also tried a number of different @ord xpath strings on the xsl:key.
The root.xml is just a test version. In production it will be much more complex.
You could use your second approach for both. So try this:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="html" omit-xml-declaration="yes" encoding="UTF-8" include-content-type="no"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:strip-space elements="*" />
<xsl:variable name="rooter" select="document('root.xml')"/>
<xsl:key name="kord" match="/root/dbu/product" use="@ord"/>
<xsl:template match="td[starts-with(@name,'rate')]/text()">{key('kord', substring-after(../@name, 'rate'), $rooter)/rate}</xsl:template>
<xsl:template match="td[starts-with(@name,'name')]/text()">{key('kord', substring-after(../@name, 'name'), $rooter)/name}</xsl:template>
</xsl:stylesheet>
The xsl:strip-space
removes the empty lines between the elements.
If you really need the xsl:for-each
for rate
, it could look like this:
<xsl:template match="td[starts-with(@name,'rate')]/text()">
<xsl:variable name="ordv" select="substring-after(../@name,'rate')"/>
<xsl:for-each select="$rooter">
<xsl:value-of select="key('kord',$ordv)/rate"/>
</xsl:for-each>
</xsl:template>
Its output is
<!DOCTYPE HTML><html lang="en">
<head>
<title>PZBank</title>
</head>
<body>
<table style="width:100%">
<tr>
<th align="left">Product #1</th>
<th align="left">Product #2</th>
<th align="left">Product #3</th>
</tr>
<tr>
<td name="name0">Amazing</td>
<td name="name1">Incredible</td>
<td name="name2"></td>
</tr>
<tr>
<td name="rate0">4.5</td>
<td name="rate1">6.6</td>
<td name="rate2"></td>
</tr>
</table>
</body>
</html>