Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用XSLT根据返回的XML数据检查单选按钮_Xml_Xslt_Radio_Checked - Fatal编程技术网

如何使用XSLT根据返回的XML数据检查单选按钮

如何使用XSLT根据返回的XML数据检查单选按钮,xml,xslt,radio,checked,Xml,Xslt,Radio,Checked,嘿,我对XSLT还很陌生,我试图用XSLT来生成和填充表单。我已经制作了XSLT,可以从服务器获取XML数据,并且我认为我已经正确地将所有字段等与其各自的XML数据进行了匹配。但是,我无法根据存储的XML数据确定如何使单选按钮和复选框处于选中状态。我在网上找到了一些类似的帖子,但我无法让它们正常运行,我希望这里的人能帮我一把 我已经编写了一些小的测试代码来尝试让它工作,因为我不想冒险弄乱完整的表单。测试XML和XSL如下所示 XML Y 布拉布拉布拉赫 XSL 选中的 单选按钮1 选中的

嘿,我对XSLT还很陌生,我试图用XSLT来生成和填充表单。我已经制作了XSLT,可以从服务器获取XML数据,并且我认为我已经正确地将所有字段等与其各自的XML数据进行了匹配。但是,我无法根据存储的XML数据确定如何使单选按钮和复选框处于选中状态。我在网上找到了一些类似的帖子,但我无法让它们正常运行,我希望这里的人能帮我一把

我已经编写了一些小的测试代码来尝试让它工作,因为我不想冒险弄乱完整的表单。测试XML和XSL如下所示

XML


Y
布拉布拉布拉赫
XSL


选中的
单选按钮1
选中的
单选按钮2


如果XML存储的值为Y,我希望在生成的HTML中选中第一个单选按钮,如果存储的值为N,则选中第二个按钮。如果有人能解释为什么这不起作用,或者这是完全错误的方法,请给我一个正确的示例,我将非常感谢。

您的xpath中出现了错误

您必须从“root/radiobuttons/radio1”中删除root/因为此时您位于根节点内,因此不再真正存在:p

<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="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="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="{./radiobuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>

选中的
单选按钮1
选中的
单选按钮2


您在xpath中出错

您必须从“root/radiobuttons/radio1”中删除root/因为此时您位于根节点内,因此不再真正存在:p

<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="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="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="{./radiobuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>

选中的
单选按钮1
选中的
单选按钮2


唉,我真不敢相信自己竟如此接近,犯了这么愚蠢的错误。谢谢你的回答。
也不起作用,因为XPath:radiotbuttons-->单选按钮中曾经而且现在仍然有一个输入错误。唉,我真不敢相信我差那么近,犯了这么愚蠢的错误。感谢您的回答。
也不起作用,因为XPath:radiotbuttons-->radiobuttons中曾经而且现在仍然有一个输入错误。
<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="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="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="{./radiobuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>