1

I am trying to display an edit button for each row if logged in as admin.

XML:

<vendors>
   <vendor id="1" name="Microsoft" description="1" odd="1"></vendor>
   <vendor id="2" name="Apple" description="1"></vendor>
   <vendor id="3" name="Google" description="1" odd="1"></vendor>
   <security ADMIN="1"></security>
</vendors>

XSLT:

<xsl:template match="vendors">
  <table>
    <thead>
      <tr>
        <xsl:if test="/vendors/security/@ADMIN = '1'">
          <td></td>
        </xsl:if>
        <td>Name</td>
        <td>Description</td>
      </tr>
    </thead>
      <tbody>
        <xsl:for-each select="vendor">
          <tr>
            <xsl:if test="@odd = '1'">
              <xsl:attribute name="class">odd</xsl:attribute>
            </xsl:if>
            <xsl:if test="/vendors/security/@ADMIN = '1'">
              <th><a href="#"><img src='edit.gif'></a></th>
            </xsl:if>
            <td title='Name'><xsl:value-of select="@name" /></td>
            <td title='Description'><xsl:value-of select="@description" /></td>
          </tr>
        </xsl:for-each>
      </tbody>
    </table>
  </div>
</xsl:template>

Right now this doesn't work. When logged in as admin (@ADMIN = "1"), everything works. When logged in with @ADMIN = "", none of the table cells are displayed (only empty rows).

2
  • minro issue but the img tag should be self closing to be valid xml Commented Aug 9, 2011 at 8:21
  • I've run this through the microsoft xml parser and it performs as expected. What XSLT parser are you using? Commented Aug 9, 2011 at 8:25

1 Answer 1

1

Apart a problem in the output document (not closed img tag and not open div) the XSLT shown is correct and should show the cells Name/Description even when @ADMIN is "" (tested using Saxon).

You might also compare integers directly, as follows:

test="/vendors/security/@ADMIN = 1"
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, it does work and I have no idea why it didn't before when I tried it many times in different browsers. Thanks.
It was the <img> tag! The <div> was just missing in my modified example, but when I closed the <img> tag it started working.
What's weird is that the <img> tag works without closing outside of the if statement.
It's always good practice close tags, even if HTML allows you to leave some of them open. That's no relevant from the point of view of a browser, while it's really relevant from the point of view of an XSLT processor. Finally, I suspect your XSLT processor is buggy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.