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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to filter on for-each?

Status
Not open for further replies.

BeckD

Programmer
Apr 16, 2002
53
GB
Hi

I am trying to filter a for-each loop with the filter being the category from the outer for-each loop. See code below:

Code:
<xsl:for-each select=&quot;/page/doccats&quot;>
	<b><xsl:value-of select=&quot;.&quot; /></b><br />
	<xsl:for-each select=&quot;/page/document[category=/page/doccats]&quot;>
			<i><xsl:value-of select=&quot;title&quot; /></i><br />
	</xsl:for-each>
</xsl:for-each>

This code doesn't work - can anybody help?

In case I haven't explained myself very well, the xml file looks like:

Code:
<page>
        <doccats>Test</doccats>
	<doccats>Welcome</doccats>
	<document>
		<docid>2</docid>
		<date>11/06/02</date>
		<title>Beck Test</title>
		<category>Test</category>
		<internal/>
	</document>
	<document>
		<docid>3</docid>
		<date>12/06/02</date>
		<title>Rhubarb</title>
		<category>Test</category>
		<internal/>
	</document>
        <document>
		<docid>4</docid>
		<date>12/06/02</date>
		<title>Welcome to the site</title>
		<category>Welcome</category>
		<internal/>
	</document>
</page>
 
I've solved this problem by doing:

Code:
<xsl:for-each select=&quot;/page/doccats&quot;>
	<xsl:variable name=&quot;vcat&quot; select=&quot;.&quot;/>
	<b><xsl:value-of select=&quot;$vcat&quot; /></b><br />
	<xsl:for-each select=&quot;/page/document[category=$vcat]&quot;>
		<i><xsl:value-of select=&quot;title&quot; /></i><br />	
	</xsl:for-each>
</xsl:for-each>

Unless anyone has any better ways of doing it?
 
<xsl:for-each select=&quot;/page/doccats&quot;>
<b><xsl:value-of select=&quot;.&quot; /></b><br/>
<xsl:for-each select=&quot;parent::page/document[category=current()]&quot;>
<b><i><xsl:value-of select=&quot;title&quot; /></i></b><br/>
</xsl:for-each>
</xsl:for-each>

would also do it with slightly less of an overhead creating that variable.

hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top