I've included two examples below, I need example 1 to work. I'm
working in MSXML 4, thanks in advance, matt
<?xml version="1.0"?>
<Navigation>
<Link>
<ParentID>0</ParentID>
<ItemID>1</ItemID>
</Link>
<Link>
<ParentID>0</ParentID>
<ItemID>2</ItemID>
</Link>
<Link>
<ParentID>1</ParentID>
<ItemID>3</ItemID>
</Link>
</Navigation>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml"/>
<xsl:template match="/">
<!-- example 1 -->
<xsl:variable name="test1">
<!-- copy elements with ParentID = 0 into 'test1' variable -->
<xsl:for-each select="//Link[ParentID = 0]">
<xsl:copy>
<xsl:copy-of select="@*|*"/>
</xsl:copy>
</xsl:for-each>
</xsl:variable>
<!-- iterate through list, look for elements with ParentID equal to
current Link -->
<!-- also i'm not sure why i need the /Link -->
<xsl:for-each select="msxsl:node-set($test1)/Link">
<xsl:variable name="id" select="ItemID"/>
<!-- should return 1 when $id = 1, but instead it only looks within
the variable, not the entire document -->
<xsl:value-of select="count(//Link[ParentID = $id])"/>
</xsl:for-each>
<!-- example 2 -->
<xsl:variable name="test2" select="//Link[ParentID = 0]"/>
<!-- this will work -->
<xsl:for-each select="msxsl:node-set($test2)">
<xsl:variable name="id" select="ItemID"/>
<xsl:value-of select="count(//Link[ParentID = $id])"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Your XML:
<?xml version="1.0"?>
<Navigation>
<Link>
<ParentID>0</ParentID>
<ItemID>1</ItemID>
</Link>
<Link>
<ParentID>0</ParentID>
<ItemID>2</ItemID>
</Link>
<Link>
<ParentID>1</ParentID>
<ItemID>3</ItemID>
</Link>
</Navigation>
With This XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes"/>
<!-- Variable to hold the root document -->
<xsl:variable name="root-document" select="/"/>
<xsl:template match="/">
<!-- example 1 -->
<xsl:variable name="test1">
<!-- copy elements with ParentID = 0 into 'test1' variable -->
<xsl:for-each select="//Link[ParentID = 0]">
<xsl:copy>
<xsl:copy-of select="@*|*"/>
</xsl:copy>
</xsl:for-each>
</xsl:variable>
<!-- iterate through list, look for elements with ParentID
equal to current Link -->
<!-- also i'm not sure why i need the /Link -->
<html>
<head/>
<body>
<xsl:for-each select="msxsl:node-set($test1)/Link">
<xsl:variable name="id" select="ItemID"/>
<!-- should return 1 when $id = 1, but instead it only looks
within the variable, not the entire document -->
<p>Link with ID=<xsl:value-of select="$id"/>
<br/>
Count of links with ParentID of <xsl:value-of select="$id"/> =
<xsl:value-of select="count($root-document//Link[ParentID = $id])"/>
</p>
</xsl:for-each>
</body>
</html>
</xsl:template>
<!-- Stop extraneous text output -->
<xsl:template match="text()">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Produces:
<html>
<head />
<body>
<p>Link with ID=1
<br />
Count of links with ParentID of 1 = 1
</p>
<p>Link with ID=2
<br />
Count of links with ParentID of 2 = 0
</p>
</body>
</html>
Which might be what you expect?
Chris.
"Matthew Sickler" <sic...@iit.edu> wrote in message
news:fe09cb3e.04062...@posting.google.com...
would you happen to know the distinction between example 1 and 2, as
far as why it's necessary to set the select query like so
"msxsl:node-set($test1)/Link" for example 1, in order to iterate over
the result set using a for-each
but if you use this, like in example 2
<xsl:variable name="test2" select="//Link[ParentID = 0]"/>
i isn't necessary to include the additional "/Link" like so
"msxsl:node-set($test2)"
they both contain the same result-tree fragment, any help would be
appreciate, i guess i'm just curious
thanks, matt
"Chris Barber" <ch...@blue-canoe.co.uk.NOSPAM> wrote in message news:<OY2F7ekW...@TK2MSFTNGP10.phx.gbl>...
When in doubt get Xselerator out and run the transform in debug mode so you can stop and inspect
what's going on.