使用 XSLT 在选择字段中显示 XML 数据

发布于 2024-12-12 09:40:17 字数 2666 浏览 2 评论 0原文

我确信这是另一个 XSLT 问题,有一个相当简单的解决方案,但我再次没有在网上看到任何关于我能够使用的主题的有用线索。我有一个 XSLT 表单,其中包含一堆选择字段,我需要将这些字段设置为在发送查询时显示数据库中 XML 中包含的数据。我目前正在使用小型示例代码进行 XML 和 XSL 测试。测试 XML 如下所示

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test2.xsl"?>
<root>
<radiobuttons>
    <radio1>Y</radio1>
    <blurb>blahblahblah</blurb>
    <hidden>N</hidden>
    <check1>Y</check1>
    <select1>3</select1>
</radiobuttons>
</root>

下面是我与该 XML 一起使用的 XSLT。我已经设置了复选框和单选按钮,以使用 xsl:if 命令根据 XML 中的数据检查自身。测试隐藏的 div 还使用相同的 xsl:if 显示依赖于 XML 数据。输入字段仅显示其目标的 XML 数据。

<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

<div> <xsl:if test="radiobuttons/hidden='N'">
<xsl:attribute name="style">display:none</xsl:attribute></xsl:if> Yaddah Yaddah Yaddah
</div>

<input type="checkbox" name="check1" value="Y">
<xsl:if test="radiobuttons/check1='Y'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Checkbox test

<label for="select1">select1:</label>
<select id="select1" ><xsl:value-of select="radiobuttons/select1"></xsl:value-of>
  <option value="1">1</option>  <option value="2">2</option>  <option value="3">3</option>  <option value="4">4</option>
  </select>
  <br></br>

<br/>
<input name="blurb" type="text" id="blurb" value="{./radiobuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>

但是我不认为我可以使用 xsl:if 正确地创建选择字段函数,因为它是布尔值。我想我可能可以为每个选项编写一个 xsl:if 命令来产生所需的结果,但这似乎是一种非常火腿的方法来完成这项工作,我确信一定有一个更优雅的解决方案。我也没有成功地使用输入字段的值方法来使其工作。

从上面的示例代码中,我希望选择字段显示 XML 中注明的第三个选项。任何人都可以建议,或者更好地给我一个例子,如何将选择字段编码到我的 XSLT 中,该字段将以上述方式运行?任何帮助将非常感激!

I'm sure this is another XSLT question with a fairly simple solution but again I haven't seen any helpful thread around the net on the topic that I have been able to use. I have an XSLT form containing a bunch of select fields that I need to set to show the data contained within XML from our database, when a query is sent. I am currently using small example codes for the XML and XSL testing. The test XML is as follows

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test2.xsl"?>
<root>
<radiobuttons>
    <radio1>Y</radio1>
    <blurb>blahblahblah</blurb>
    <hidden>N</hidden>
    <check1>Y</check1>
    <select1>3</select1>
</radiobuttons>
</root>

Below is the XSLT I am using with this XML. I have set up both the checkboxes and the radio buttons to check themselves dependant upon the data within the XML using xsl:if commands. The test hidden div also displays dependant on XML data using the same xsl:if. The input field just displays the XML data it is targeted at.

<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

<div> <xsl:if test="radiobuttons/hidden='N'">
<xsl:attribute name="style">display:none</xsl:attribute></xsl:if> Yaddah Yaddah Yaddah
</div>

<input type="checkbox" name="check1" value="Y">
<xsl:if test="radiobuttons/check1='Y'">
<xsl:attribute name="checked">checked</xsl:attribute></xsl:if>
</input>Checkbox test

<label for="select1">select1:</label>
<select id="select1" ><xsl:value-of select="radiobuttons/select1"></xsl:value-of>
  <option value="1">1</option>  <option value="2">2</option>  <option value="3">3</option>  <option value="4">4</option>
  </select>
  <br></br>

<br/>
<input name="blurb" type="text" id="blurb" value="{./radiobuttons/blurb}"></input>
</xsl:template>
</xsl:stylesheet>

However I don't think I can make a select field function correctly using xsl:if because it is Boolean. I think I could probably write an xsl:if command for every option which would produce the desired result, however that seems like a very ham handed method of making this work and I'm sure there must be a much more elegant solution to this. I also haven't had any success making it work using the value method for the input field.

From the example codes above I would like the select field to show the 3rd option as noted in the XML. Can anyone suggest, or even better give me an example of, how I could code a select field into my XSLT that will behave in the manner described above? Any help would be really gratefully received!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2024-12-19 09:40:17

您可以做的是将 select 语句的所有四个选项放入 XSLT 中,充当一种查找表(其中 my 命名空间必须在 XSLT 的顶部定义) )

<my:options> 
  <option>1</option> 
  <option>2</option> 
  <option>3</option> 
  <option>4</option> 
</my:options> 

然后,要访问 XSLT 中的这些选项,您可以创建一个变量,如下所示...

<xsl:variable name="options" select="document('')//my:options/*"/> 

然后,要呈现您的 select 语句,只需循环访问选项即可。尽管仍使用 xsl:if,但您只需编写一次代码。

  <select id="select1">
     <xsl:for-each select="$options">
        <option value="{.}">
           <xsl:if test=". = $select1">
              <xsl:attribute name="selected">true</xsl:attribute>
           </xsl:if>
           <xsl:value-of select="."/>
        </option>
     </xsl:for-each>
  </select>

这是删节的 XSLT(仅包含选择代码)

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="mynamespace"
 exclude-result-prefixes="my">

   <xsl:output method="html" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/>

   <my:options>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
   </my:options>
   <xsl:variable name="options" select="document('')//my:options/*"/>

   <xsl:template match="/">
      <HTML>
         <BODY>
            <form>
               <xsl:apply-templates select="root"/>
            </form>
         </BODY>
      </HTML>
   </xsl:template>

   <xsl:template match="root">
      <xsl:variable name="select1" select="radiobuttons/select1"/>
      <select id="select1">
         <xsl:for-each select="$options">
            <option value="{.}">
               <xsl:if test=". = $select1">
                  <xsl:attribute name="selected">true</xsl:attribute>
               </xsl:if>
               <xsl:value-of select="."/>
            </option>
         </xsl:for-each>
      </select>
   </xsl:template>
</xsl:stylesheet>

当应用于示例 XML 时,输出如下

<HTML>
<BODY>
<form>
<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="true">3</option>
<option value="4">4</option>
</select>
</form>
</BODY>
</HTML>

What you could do, is put all four options for your select statement in your XSLT to act as a sort of look-up table (where the my namespace would have to be defined at the top of your XSLT)

<my:options> 
  <option>1</option> 
  <option>2</option> 
  <option>3</option> 
  <option>4</option> 
</my:options> 

Then, to access these options in the XSLT, you can create a variable, like so...

<xsl:variable name="options" select="document('')//my:options/*"/> 

Then, to render your select statement, it is just a case of looping through the options. Although an xsl:if is still used, you only have to code it once.

  <select id="select1">
     <xsl:for-each select="$options">
        <option value="{.}">
           <xsl:if test=". = $select1">
              <xsl:attribute name="selected">true</xsl:attribute>
           </xsl:if>
           <xsl:value-of select="."/>
        </option>
     </xsl:for-each>
  </select>

Here is the abridged XSLT (containing only the code for the select)

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="mynamespace"
 exclude-result-prefixes="my">

   <xsl:output method="html" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/>

   <my:options>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
   </my:options>
   <xsl:variable name="options" select="document('')//my:options/*"/>

   <xsl:template match="/">
      <HTML>
         <BODY>
            <form>
               <xsl:apply-templates select="root"/>
            </form>
         </BODY>
      </HTML>
   </xsl:template>

   <xsl:template match="root">
      <xsl:variable name="select1" select="radiobuttons/select1"/>
      <select id="select1">
         <xsl:for-each select="$options">
            <option value="{.}">
               <xsl:if test=". = $select1">
                  <xsl:attribute name="selected">true</xsl:attribute>
               </xsl:if>
               <xsl:value-of select="."/>
            </option>
         </xsl:for-each>
      </select>
   </xsl:template>
</xsl:stylesheet>

When applied to your sample XML, the following is output

<HTML>
<BODY>
<form>
<select id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="true">3</option>
<option value="4">4</option>
</select>
</form>
</BODY>
</HTML>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文