我已经有一段时间没有编码了,一位朋友向我寻求帮助。我似乎无法理解它。这是我正在尝试修复的旧代码。
基本上,我正在尝试制作一个为父级的每个子级缩进的表格。
我可以让它正确缩进第一个孩子,然后它就停止了。 有人可以指出我可以在哪里阅读此内容吗?
*更新
我已经尽可能地添加了代码。工作休息时补充。 下班后如有需要会更新。
数据 - 价值观已经改变,因为我一直感觉零食。
<foods>
<name>Junk Food</name>
<qty>1</qty>
<children>
<food>
<name>Gummies</name>
<qty>7</qty>
<children>
<food>
<name>Chocolate</name>
<qty>5</qty>
<children />
</food>
</children>
<children>
<food>
<name>Sour</name>
<qty>2</qty>
<children />
</food>
</children>
<food>
<name>Chips</name>
<qty>8</qty>
<children>
<food>
<name>Salt & Vinegar</name>
<qty>5</qty>
<children />
</food>
<food>
<name>Lightly Salted</name>
<qty>1</qty>
<children>
<food>
<name>Rippled</name>
<qty>2</qty>
<children>
</food>
</children>
</food>
<children />
</food>
<food>
<name>Fruit</name>
<qty>18</qty>
<children>
<food>
<name>Pineapple</name>
<qty>5</qty>
<children />
</food>
<food>
<name>Apple</name>
<qty>13</qty>
<children>
<food>
<name>Macintosh</name>
<qty>8</qty>
<children />
</food>
<food>
<name>Golden</name>
<qty>2</qty>
<children />
</food>
<food>
<name>Granny Smith</name>
<qty>3</qty>
<children />
</food>
</children>
</food>
</children>
</food>
</children>
</foods>
**代码**
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Character sheet based on the Shadowrun 4th Edition Character Sheet -->
<!-- Created by Keith Rudolph, [email protected] -->
<!-- Version -892 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:include href="ConditionMonitor.xslt"/>
<xsl:template match="/characters/character">
<xsl:text disable-output-escaping="yes"><![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">]]></xsl:text>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title><xsl:value-of select="name" /></title>
<style type="text/css">
*
{
font-family: segoe condensed, tahoma, trebuchet ms, arial;
font-size: 8pt;
text-align: left;
}
.tableborder
{
border: solid 2px #1c4a2d;
}
.indent
{
padding-left: 20px;
}
</style>
</head>
<body>
<div class="block" id="foodBlock">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr><td width="70%" class="tableborder" style="text-align:center;" valign="top">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="55%">
<strong>NAME</strong>
</td>
<td width="15%" style="text-align:center;">
<strong>QTY.</strong>
</td>
</tr>
<xsl:call-template name="food1" />
</table>
</td>
</tr>
</table>
</div>
<xsl:template name="foodplugin">
<xsl:param name="food" />
<xsl:for-each select="children/food">
<xsl:sort select="name" />
<xsl:sort select="foodname" />
<xsl:value-of select="qty" /><xsl:text>x </xsl:text>
<xsl:value-of select="name" />
<xsl:if test="children/food">
<br /><xsl:text>  </xsl:text>
<xsl:call-template name="foodplugin">
<xsl:with-param name="food" select="." />
</xsl:call-template>
</xsl:if>
<xsl:if test="position() != last()"><br /></xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template name="food1">
<xsl:variable name="halfcut" select="count(foods/food)"/>
<xsl:variable name="sortedcopy">
<xsl:for-each select="foods/food">
<xsl:sort select="food" />
<xsl:sort select="name" />
<xsl:if test="position() <= $halfcut">
<xsl:copy-of select="current()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="msxsl:node-set($sortedcopy)/food">
<xsl:choose>
<xsl:when test="position() = 1">
<tr><td colspan="3" style="border-bottom:solid black 1px;"><strong><xsl:value-of select="location" /></strong></td></tr>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="location != preceding-sibling::food[1]/location">
<tr><td colspan="3" style="border-bottom:solid black 1px;"><strong><xsl:value-of select="location" /></strong></td></tr>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
<tr>
<xsl:if test="position() mod 2 != 1">
<xsl:attribute name="bgcolor">#e4e4e4</xsl:attribute>
</xsl:if>
<td width="80%" valign="top">
<xsl:value-of select="name" />
</td>
<td width="10%" style="text-align:center;" valign="top">
<xsl:choose>
<xsl:when test="children/food != ''"> </xsl:when>
<xsl:otherwise><xsl:value-of select="qty" /></xsl:otherwise>
</xsl:choose>
</td>
</tr>
<xsl:if test="children/food">
<tr>
<xsl:if test="position() mod 2 != 1">
<xsl:attribute name="bgcolor">#e4e4e4</xsl:attribute>
</xsl:if>
<td colspan="3" class="indentfood">
<xsl:call-template name="foodplugin">
<xsl:with-param name="food" select="." />
</xsl:call-template>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
想要的
Gummies
Chocolate
Sour
Chips
Salt & Vinegar
Lightly Salted
Rippled
Fruit
Pineapple
Apple
Granny Smith
BC
California
Golden
Macintosh
结果
Gummies
Chocolate
Sour
Chips
Salt & Vinegar
Lightly Salted
Rippled
Fruit
Pineapple
Apple
Granny Smith
BC
California
Golden
Macintosh
这是一个简单的样式表,它使用 XSLT 的递归处理模型来生成嵌套的 HTML 无序列表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="/ | children">
<ul>
<xsl:apply-templates select="food"/>
</ul>
</xsl:template>
<xsl:template match="food">
<li>
<xsl:value-of select="name"/>
<xsl:apply-templates select="children"/>
</li>
</xsl:template>
</xsl:stylesheet>
给定一个格式良好的(!!!) XML 输入:
<food>
<name>Junk Food</name>
<qty>1</qty>
<children>
<food>
<name>Gummies</name>
<qty>7</qty>
<children>
<food>
<name>Chocolate</name>
<qty>5</qty>
<children />
</food>
<food>
<name>Sour</name>
<qty>2</qty>
<children />
</food>
</children>
</food>
<food>
<name>Chips</name>
<qty>8</qty>
<children>
<food>
<name>Salt & Vinegar</name>
<qty>5</qty>
</food>
<food>
<name>Lightly Salted</name>
<qty>1</qty>
<children>
<food>
<name>Rippled</name>
<qty>2</qty>
<children/>
</food>
</children>
</food>
</children>
</food>
<food>
<name>Fruit</name>
<qty>18</qty>
<children>
<food>
<name>Pineapple</name>
<qty>5</qty>
<children />
</food>
<food>
<name>Apple</name>
<qty>13</qty>
<children>
<food>
<name>Macintosh</name>
<qty>8</qty>
<children/>
</food>
<food>
<name>Golden</name>
<qty>2</qty>
<children/>
</food>
<food>
<name>Granny Smith</name>
<qty>3</qty>
<children/>
</food>
</children>
</food>
</children>
</food>
</children>
</food>
(渲染的)结果将是:
您可以添加 CSS 代码来进一步设置结果的样式。
这是一个更简单的样式表,它生成由制表符缩进的 text 结果。这还使用 XSLT 的本机递归来增加每个级别的缩进。
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="food">
<xsl:param name="indent"/>
<xsl:value-of select="$indent"/>
<xsl:value-of select="name"/>
<xsl:text> </xsl:text>
<xsl:apply-templates select="children/food">
<xsl:with-param name="indent" select="concat($indent, '	')"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
使用与我之前的答案中相同的 XML 示例输入,这里的结果将是:
Junk Food
Gummies
Chocolate
Sour
Chips
Salt & Vinegar
Lightly Salted
Rippled
Fruit
Pineapple
Apple
Macintosh
Golden
Granny Smith