Re: [xsl] Traversing nodes and storing a matched value

Subject: Re: [xsl] Traversing nodes and storing a matched value
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Wed, 29 Jun 2005 00:39:31 -0700 (PDT)
Hi Mat,
  You may try something like this .. (Hope I got the
requirements right)

<?xml version="1.0"?> 
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
           xmlns:common="http://exslt.org/common";
           version="1.0">
 
 <xsl:output method="text" /> 
 
 <xsl:param name="siteID" select="'1'" />
 
 <xsl:template match="/"> 
   <xsl:variable name="rtf">
     <xsl:for-each select="//exsites">
       <xsl:choose>
         <xsl:when test=". = $siteID">
           <menu name="{parent::menu/@name}">
             <process x="yes" />
           </menu>
         </xsl:when>
         <xsl:otherwise>
           <menu name="{parent::menu/@name}">
	      <process x="no" />
           </menu>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:for-each>
   </xsl:variable>
   
   <xsl:for-each
select="common:node-set($rtf)/menu/process[@x =
'yes']">
     <!-- do something -->
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

This example uses node-set function.. and tested with
Saxon 8.4.

Regards,
Mukul


--- Mat Bergman <matbergman@xxxxxxxxx> wrote:

> My efforts to streamline my stylesheet into
> something
> well-formed are paying off with help from this list.
> I
> am currently working with XML input that looks like
> this:
> 
> <menudata>
> 	<menu name="link1">
> 	<exsites>0</exsites>	
> 	</menu>
> 	<menu name="link2">
> 	<exsites>0</exsites>
> 		<menu name="link2a">
> 		<exsites>1</exsites>
> 		<exsites>2</exsites>
> 		</menu>
> 	</menu>
> </menudata>
> 
> The value of the <exsites> element determines if its
> parent <menu> element will appear in the output,
> based
> on a $siteID parameter set in the stylesheet. In
> some
> cases, <menu> has multiple <exsites> children.
> 
> I'm not clear how to use XSL to say, "if the value
> of
> ANY <exsite> matches $siteID, then flag its parent's
> node for different processing". Here's my attempt
> (the
> section where I'm in trouble is commented):
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="xml" indent="yes"/>
> <xsl:strip-space elements="*"/>
> 
> <!-- siteID determines if a link is displayed for a
> specific cobrand -->
> <xsl:param name="siteID" select="2"/>
> 
> <xsl:template match="/menudata">
> 	<xsl:call-template name="write-menu">
> 		<xsl:with-param name="items" select="menu"/>
> 	</xsl:call-template>
> </xsl:template>
> 
> <xsl:template name="write-menu">
> 	<xsl:param name="items" select="/.."/>
> 
> 	<ul>
> 		<xsl:for-each select="$items">
> 			<xsl:call-template name="parseExclusions"/>
> 		</xsl:for-each>
> 	</ul>
> 
> </xsl:template>
> 
> <xsl:template name="parseExclusions">
> 
> <!-- Check <exsites> for a match against $siteID -
> if
> a match, don't display -->
> 	<xsl:for-each select="exsites">
> 		<xsl:if test="current()=$siteID">
> 
> <!-- This is where I need the stylesheet not to
> write
> the link if any of the <exsites> values match the
> $siteID parameter -->
> <!-- For now I'm just writing out the value if it
> matches $siteID -->
> 		<xsl:value-of select="current()"/>
> 		</xsl:if>
> 	</xsl:for-each>
> 
> 		<li>
> 			<xsl:value-of select="@name"/>
> 			<xsl:if test="menu">
> 				<xsl:call-template name="write-menu">
> 					<xsl:with-param name="items" select="menu"/>
> 				</xsl:call-template>
> 			</xsl:if>
> 		</li>
> 
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> 
> My instinct is to set a boolean variable if any
> <exsite> element matches, then use that variable to
> control the output. Something like this (in
> pseudo-code):
> 
> <xsl:for-each select="exsites">
> 	<xsl:if test="current()=$siteID">
> 	<!-- set variable - var excludeFlag = 1 -->
> 	</xsl:if>
> </xsl:for-each>
> 
> <!-- write tag - if (excludeFlag!=1) -->
> <li>
> <xsl:value-of select="@name"/>
> 
> </li>
> <!-- /if -->
> 
> But of course an XSL variable is a different beast.
> That seems to be my real weakness in XSL, grasping
> the
> concept of templates over variables.
> 
> Am I on the right track?
> 
> Thanks,
> 
> -Mat
> 
> 
> 
> 
> 		
> ____________________________________________________
> 
> Yahoo! Sports 
> Rekindle the Rivalries. Sign up for Fantasy Football
> 
> http://football.fantasysports.yahoo.com
> 
> 



		
____________________________________________________ 
Yahoo! Sports 
Rekindle the Rivalries. Sign up for Fantasy Football 
http://football.fantasysports.yahoo.com

Current Thread