【发布时间】:2018-04-26 14:32:24
【问题描述】:
我今天开始了我的第一次 xsl 冒险,希望能快速完成这个“简单”的脚本。 现在 4 小时后我仍然不知道为什么它会生成 2 次...
我需要您的帮助来查找我的脚本中的错误,因为我自己找不到...
我正在使用 XSL 创建基于 XML 的 HTML 表单。
我基本上有一个自动创建两次的模板。我只想要<div class="columnDiv">中的那个。
屏幕截图 error reasult。
屏幕截图 achieve。
我的 XML
<?xml version="1.0" encoding="UTF-8"?>
<labels>
<options>
<option type="Top Text 1"/>
<option type="Top Text 2"/>
<option type="Top Text 2"/>
</options>
<statuses>
<status type="Correct">
<answer description="Yes"/>
<answer description="No"/>
</status>
<status type="Error - Type 1">
<answer description="Option 1"/>
<answer description="Option 2"/>
<answer description="Option 3"/>
<answer description="Option 4"/>
</status>
<status type="Error - Type 2">
<answer description="Option 1"/>
<answer description="Option 2"/>
<answer description="Option 3"/>
<answer description="Option 4"/>
</status>
</statuses>
</labels>
我的 XSL
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<form method="POST" action="new-process.php">
<div class="mainC">
<xsl:apply-templates/>
</div>
<input type="submit" name="submit" value="Submit" />
</form>
</xsl:template>
<xsl:template match="options">
<xsl:for-each select="option">
<div class="columnDiv">
<h3><xsl:value-of select="@type"/></h3>
<xsl:apply-templates select="/labels/statuses"/>
</div>
</xsl:for-each>
</xsl:template>
<xsl:template match="status">
<xsl:for-each select=".">
<label><xsl:value-of select ="@type"/></label>
<select>
<xsl:for-each select="answer">
<option><xsl:value-of select="@description"/></option>
</xsl:for-each>
</select>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
当前输出以及所需输出的注释
<?xml version="1.0" encoding="UTF-8"?>
<form method="POST" action="new-process.php">
<div class="mainC">
<div class="columnDiv">
<h3>Top Text 1</h3>
<label>Correct</label><select><option>Yes</option><option>No</option></select>
<label>Error - Type 1</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
<label>Error - Type 2</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
</div>
<div class="columnDiv">
<h3>Top Text 2</h3>
<label>Correct</label><select><option>Yes</option><option>No</option></select>
<label>Error - Type 1</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
<label>Error - Type 2</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
</div>
<div class="columnDiv">
<h3>Top Text 2</h3>
<label>Correct</label><select><option>Yes</option><option>No</option></select>
<label>Error - Type 1</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
<label>Error - Type 2</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
</div>
<!-- \ this is the output that I do no want \ -->
<label>Correct</label><select><option>Yes</option><option>No</option></select>
<label>Error - Type 1</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
<label>Error - Type 2</label><select><option>Option 1</option><option>Option 2</option><option>Option 3</option><option>Option 4</option></select>
<!-- / this is the output that I do no want / -->
</div>
<input type="submit" name="submit" value="Submit"/>
</form>
【问题讨论】:
-
您的第一个 Apply-Templates 没有过滤器;所以应用更多的模板而不仅仅是选项。添加
select="/labels/options"修复。