[xsl] Traversing nodes and storing a matched value

Subject: [xsl] Traversing nodes and storing a matched value
From: Mat Bergman <matbergman@xxxxxxxxx>
Date: Tue, 28 Jun 2005 20:52:17 -0700 (PDT)
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

Current Thread