无法使用 if 条件在 xsl 中条件渲染按钮

问题描述 投票:0回答:1

内容.xml

<?xml version="1.0" encoding="UTF-8"?>
<Data>
  <Datum ID="D02" Type="Boolean" Name="userGroup">tr</Datum>
</Data>

外观.xsl

<?xml version="1.0"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:template match="/">
    <html>

    <body>

      <span id="userGroup"><xsl:value-of select="/Data/Datum[@ID='D02']"/></span>


      <xsl:variable name="userGroupValue" select="//span[@id='userGroup']/text()" />


      <xsl:if test="$userGroupValue = 'true'">
        <button id="myButton" style="background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;">Click Me</button>
      </xsl:if>
      <xsl:if test="$userGroupValue != 'true'">
        <button id="myButton" style="background-color: #ccc; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: not-allowed;">Click Me</button>
      </xsl:if>
    </body>

    </html>
  </xsl:template>

</xsl:stylesheet>

问题: 我编写了一段代码,使用 Content.xml 文件中的

span
标签获取值(true/false)。现在,我将该值存储在
span
标记值的 xsl: 变量中。然后我在代码中使用 xsl:if 有条件地渲染按钮。为了比较数据,我在变量中添加了名称
userGroupValue
,通过它我们可以将值放入 xsl:if 中,然后有条件地渲染按钮。

但是在尝试了很多更改之后,我无法有条件地渲染按钮。 如果有人知道 xsl,请帮助我。

xml xslt xslt-1.0 xslt-2.0
1个回答
0
投票

这个:

select="//span[@id='userGroup']/text()"

不会选择任何内容,因为它不存在于源代码树中;您刚刚创建了它并且它位于结果树中。

尝试将您的选择更改为(与用于创建跨度的 xsl:value 相同):

select="/Data/Datum[@ID='D02']"
© www.soinside.com 2019 - 2024. All rights reserved.