我有一本包含以下值的字典:
<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>
最佳答案
我在这里进行猜测,但是通过查看您的 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>
关于c# - Foreach 在 xslt 中带有嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13376560/