tr Appearance.xsl tr Appearance.xsl tr Appearance.xsl

Unable to conditional render button in xsl using if condition

27 views Asked by At

Content.xml

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

Appearance.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>

Question: I wrote a code to get the value using the span tag from the Content.xml file (true/false). Now I was storing that value in the xsl: variable from the span tag value. Then I was conditionally rendering the button using xsl:if in the code. For comparing the data I added name in the variable as userGroupValue through which we can get the value into our xsl:if and then conditionally render the button.

But after trying a lot of changes too, I'm unable to conditionally render the button. Please help me out if anyone know xsl.

1

There are 1 answers

0
Daniel Haley On

This:

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

is not going to select anything because it doesn't exist in the source tree; you just created it and it's in the result tree.

Try changing your select to (it's the same as the xsl:value-of you used to create the span):

select="/Data/Datum[@ID='D02']"

Note: The value of Datum is tr and not true in your example. Not sure if that's a typo or not but those would also need to match when doing the comparison to $userGroupValue.