Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL:Attribute testing a sub Node

Status
Not open for further replies.

jquaid

IS-IT--Management
Aug 7, 2001
15
IE
Hi,

I've been trying all day to get the following code to work and keep on encountering problems - is there a better way of doing it?

I have a Menu node which contains an allowedUserRoles node. This sub-node may have multiple authority/role attributes. I pass in an authority parameter to the XSL and onl;y want to continue processing the Node if the authority/role attribue is blank, or if one of the authority/role attributes matches the parameter. I have created an attribute to store the true/false, but when I test the attribute's value in the XSL, it is blank - although the correct value is appearing in the generated HTML???
Any help/suggestions would be greatly appreciated.
Jeremy

The XML looks as follows:
Code:
<menus>
  <menu id=&quot;eNew&quot;>
     <description>Main Menu</description> 
     <allowedUserRole>
        <authority /> 
     </allowedUserRole>
     <contents>
        <menu id=&quot;e1&quot;>
          <description>Customers</description> 
          <allowedUserRole>
             <authority>
                <role>user2</role> 
             </authority>
             <authority>
                <role>manager</role> 
             </authority>
          </allowedUserRole>
        </menu>
     </contents>
  </menu>
</menus>

The XSL code is:
Code:
<xsl:template match=&quot;menu&quot;>
 <div onselectstart=&quot;return false&quot; ondragstart=&quot;return false&quot;>
 <xsl:attribute name=&quot;allowedItem&quot;>false</attribute>
 
 <xsl:for-each select=&quot;./allowedUserRole&quot;>
   <xsl:for-each select=&quot;./authority&quot;>
     <xsl:choose>
       <xsl:when test=&quot;string(role) = ''&quot;>
         <xsl:attribute name=&quot;allowedItem&quot;>true</xsl:attribute> 
       </xsl:when>
       <xsl:otherwise>
         <xsl:if test=&quot;string(role) = $p_Authority&quot;>
           <xsl:attribute name=&quot;allowedItem&quot;>true</xsl:variable> 
         </xsl:if>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:for-each>
 </xsl:for-each>

 <xsl:choose>
    <xsl:when test=&quot;allowedItem = 'true'&quot;>
.....continue processing node
 
Hi Jeremy,

The xls:attribute element is used to add an attribute to the preceding element in your stylesheet. So in your stylesheet you've added the attribute 'allowedItem' with the value 'false' to the html div element. (output will be: <div onselectstart=&quot;return false&quot; ondragstart=&quot;return false&quot; allowedItem=&quot;false&quot;>)


When you wish to store values in your stylesheet, use the xsl:variable element instead. You can refer to a variable in an xpath expression using it's name preceded by a $.
Code:
<xsl:variable name=&quot;test&quot;>Hi Jeremy</xsl:variable>
<xsl:value-of select=&quot;$test&quot;/>
Note however, that you can assign a value to a variable only once in a stylesheet, so that will not work for you either.

What you can do is call a template when your requirements are met and just continue when not.
Code:
     <xsl:choose>
       <xsl:when test=&quot;string(role) = ''&quot;>
          <xsl:apply-template select=&quot;.&quot;/>
       </xsl:when>
       <xsl:when test=&quot;string(role) = $p_Authority&quot;>
          <xsl:apply-template select=&quot;.&quot;/>
       </xsl:when>
     </xsl:choose>

Good luck!

Jordi Reineman
 
Hi Jordi,
Thanks for your reply. We just managed to solve it ourselves by using a variable and changing the scope:
Code:
<xsl:variable=&quot;allowedItem&quot;>
    <xsl:for-each select=&quot;./allowedUserRole&quot;>
       <xsl:for-each select=&quot;./authority&quot;>
        <xsl:choose>
          <xsl:when test=&quot;string(role) = '' or &quot;string(role) = $p_Authority&quot;>true</xsl:when>
          </xsl:choose>
       </xsl:for-each>
    </xsl:for-each>
</xsl:variable>

<xsl:choose>
    <xsl:when test=&quot;$allowedItem != ''&quot;>

We had to move the scope of the variable out to the same level as our CHOOSE test.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top