嘿,我对 XSLT 还很陌生,并试图用它们来生成和填充表单。我已经制作了 XSLT 并且可以从服务器获取 XML 数据,并且我认为我已经正确地将所有字段等与它们各自的 XML 数据进行了匹配。但是,我不知道如何根据存储的 XML 数据检查单选按钮和复选框。我在网上找到了几个类似的帖子,但我无法让它们正常运行,希望这里有人能帮我一把。
我制作了一些小的测试代码来尝试让它工作,因为我不想冒险弄乱完整的表格。测试 XML 和 XSL 如下所示。
XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test2.xsl"?>
<root>
<radiobuttons>
<radio1>Y</radio1>
<blurb>blahblahblah</blurb>
</radiobuttons>
</root>
XSL
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="no"
encoding="UTF-8"/>
<xsl:template match="/">
<HTML>
<BODY>
<form>
<xsl:apply-templates select="root"/>
</form>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="root">
<input type="radio" name="radio1" value="Y" >
<xsl:if test="root/radiobuttons/radio1='Y'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Radio Button 1
<input type="radio" name="radio1" value="N" >
<xsl:if test="root/radiobuttons/radio1='N'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Radio Button 2
<br/>
<input name="blurb" type="text" id="blurb" value="{./radiotbuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>
如果 XML 存储的值为 Y,我希望在生成的 HTML 中检查第一个单选按钮,如果存储的值为 N,则检查第二个按钮。如果有人能解释为什么这不起作用或如果这完全是错误的方法,那么请给我一个正确的例子,我将不胜感激。