我有一个包含以下值的字典:
<root
user_salutation="Geachte heer"
wg="7"
wgList-0="68"
wgListCount-0="3"
wgList-1="65"
wgListCount-1="1"
wgList-2="62"
wgListCount-2="1"
wgList-3="58"
wgListCount-3="2"
/>
如何链接列表并在foreach中一起计算?
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration ="yes" encoding="ISO-8859 1"/>
<xsl:variable name="style">
body {
font-family: verdana;
font-size: 11px;
}
td {
height: 15px;
border: 0px;
font-family: verdana;
font-size: 11px;
}
th {
background-color: #999999;
color: white;
font-family: verdana;
font-size: 11px;
}
</xsl:variable>
<xsl:variable name="prijs_zichtbaar"> 1 </xsl:variable>
<xsl:key name="params" match="tag[@name!='param']" use="generate-id(preceding- sibling::tag[@name='param'][1])" />
<xsl:template match="tag[@name='param']">
<xsl:text>param </xsl:text>
<xsl:apply-templates select="key('params', generate-id())" />
</xsl:template>
<xsl:template match="tag">
<xsl:value-of select="concat(' - ', @name, ' ')" />
</xsl:template>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>wgp</title>
<style>
<xsl:value-of select="$style"/>
</style>
</head>
<body>
<table cellpadding="3" cellspacing="0" border="0" width="" >
<colgroup>
<col width="200"/>
<col width="100"/>
<col width="100"/>
<col width="100"/>
</colgroup>
<tr>
<th align="left">Soort</th>
<th align="right">Dag</th>
<th align="right">Aantal</th>
<th align="right">Ontvangers</th>
</tr>
<xsl:for-each select="root/@*[starts-with(name(),'wgList-')]">
<xsl:element name="TR">
<xsl:if test="position() mod 2 = 0">
<xsl:attribute name="bgcolor">#CCCCCC</xsl:attribute>
</xsl:if>
<td align="left">wg</td>
<td align="right"><xsl:value-of select="."/></td>
<td align="right"><xsl:value-of select="@*[name() = concat('wgListCount-', substring-after(name(current()), '-'))]" /></td>
</xsl:element>
</xsl:for-each>
<xsl:if test="root/@list1 > 0">
<xsl:element name="TR">
<td align="left"><b>subtotaal:</b></td>
td align="left"></td>
<td align="right">----------------<br/><xsl:value-of select="root/@wg" /></td>
<td align="right">----------------<br/><xsl:value-of-select="root/@wg" /></td>
</xsl:element>
</xsl:if>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:2)
我在这里猜测,但是从查看你的XSLT片段,它建议你的键和值是 root 元素的属性,就像这样
<root
wgList-0="68"
wgListCount-0="3"
wgList-1="65"
wgListCount-1="4"
wgList-2="62"
wgListCount-2="8">
</root>
在这种情况下,您需要获取密钥值的XSLT是
<xsl:value-of
select="../@*
[name() = concat('wgListCount-', substring-after(name(current()), '-'))]"/>
这样做是获取连字符后面的当前属性名称的一部分(即0,1或2),然后使用此值获取预期的 wgListCount 元素。这假设您当前位于 wgList - 元素。
以下是一些示例XSLT(我已经删除了一些代码,只是为了保持简短并专注于手头的问题)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes" encoding="ISO-8859 1"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>wgp</title>
</head>
<body>
<table cellpadding="3" cellspacing="0" border="0" width="">
<tr>
<th align="left">Soort</th>
<th align="right">Dag</th>
<th align="right">Aantal</th>
<th align="right">Ontvangers</th>
</tr>
<xsl:for-each select="root/@*[starts-with(name(),'wgList-')]">
<xsl:element name="TR">
<td align="left">wg</td>
<td align="right">
<xsl:value-of select="."/>
</td>
<td align="right">
<xsl:value-of select="../@*[name() = concat('wgListCount-', substring-after(name(current()), '-'))]"/>
</td>
</xsl:element>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当应用于上述XML时,输出如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>wgp</title>
</head>
<body>
<table cellpadding="3" cellspacing="0" border="0" width="">
<tr>
<th align="left">Soort</th>
<th align="right">Dag</th>
<th align="right">Aantal</th>
<th align="right">Ontvangers</th>
</tr>
<TR>
<td align="left">wg</td>
<td align="right">68</td>
<td align="right">3</td>
</TR>
<TR>
<td align="left">wg</td>
<td align="right">65</td>
<td align="right">1</td>
</TR>
<TR>
<td align="left">wg</td>
<td align="right">62</td>
<td align="right">1</td>
</TR>
<TR>
<td align="left">wg</td>
<td align="right">58</td>
<td align="right">2</td>
</TR>
</table>
</body>
</html>