[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] XSL Template Match using z:row attributes


Subject: RE: [xsl] XSL Template Match using z:row attributes
From: "Michael McCarty" <mccarty@xxxxxxxxxx>
Date: Wed, 12 Dec 2001 20:22:26 -0800

Ok, you might need to ignore my previous reply.  I've been playing with
the XSL and (with the help of that Xselerator tool) I think I understand
better, but not completly.

My problem is that I'm getting duplicate output.  The XML (trimmed) is:

<?xml-stylesheet type="text/xsl" href="CategoryTree2.xsl" ?>
<xml xmlns:rs='urn:schemas-microsoft-com:rowset'
	 xmlns:z='#RowsetSchema'>
<rs:data>
	<z:row LEVEL='1' CATNAME='Level 1' CATID='1' PARENTID='0' />
	<z:row LEVEL='2' CATNAME='Level 2' CATID='3' PARENTID='1' />
	<z:row LEVEL='2' CATNAME='Level 2' CATID='4' PARENTID='1' />
	<z:row LEVEL='3' CATNAME='Level 3' CATID='5' PARENTID='4' />
	<z:row LEVEL='3' CATNAME='Level 3' CATID='6' PARENTID='4' />
	<z:row LEVEL='1' CATNAME='Level 1' CATID='21' PARENTID='0' />
	<z:row LEVEL='2' CATNAME='Level 2' CATID='22' PARENTID='21' />
</rs:data>
</xml>

The XSL is:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
		xmlns:rs='urn:schemas-microsoft-com:rowset'
		xmlns:z='#RowsetSchema'	version="1.0">

<xsl:template match="/">
	<HTML>
	<BODY topmargin="0" leftmargin="0">
	<xsl:apply-templates select="/xml/rs:data"/>
	</BODY>
	</HTML>
</xsl:template>

<xsl:template match="z:row">
<div class="clsItem">
	CatId_<xsl:value-of select="@CATID"/> - 
	<b><xsl:value-of select="@CATNAME"/></b> - 
	Parent_<xsl:value-of select="@PARENTID"/>
  <div id="container">
	<xsl:apply-templates select="../z:row[(@LEVEL=current()/@LEVEL
+ 1) and (@PARENTID=current()/@CATID)]"/>
  </div>
</div>
</xsl:template>
</xsl:stylesheet> 

Here's what I get:

CatId_1 - Level 1 - Parent_0
CatId_3 - Level 2 - Parent_1
CatId_4 - Level 2 - Parent_1
CatId_5 - Level 3 - Parent_4
CatId_6 - Level 3 - Parent_4
CatId_3 - Level 2 - Parent_1   <!-- duplicate -->
CatId_4 - Level 2 - Parent_1   <!-- duplicate -->
CatId_5 - Level 3 - Parent_4   <!-- duplicate -->
CatId_6 - Level 3 - Parent_4   <!-- duplicate -->
CatId_5 - Level 3 - Parent_4   <!-- duplicate -->
CatId_6 - Level 3 - Parent_4   <!-- duplicate -->
CatId_21 - Level 1 - Parent_0
CatId_22 - Level 2 - Parent_21
CatId_22 - Level 2 - Parent_21   <!-- duplicate -->

I think I'm going crazy learning this... =:-)
As always, your help is greatly appreciated.

Michael =:-)

---- TSchutzerWeissmann@xxxxxxxxxxxxxxxx wrote:
> Michael wrote:
> 
> >What I am getting is this:
> >
> >+ Product 1 
> >+ Product 2 
> >
> >It appears that I get the LEVEL='1', but not the remaining.  I wonder
> >if there's a way around 'hard coding' the LEVEL's in case I have more
> >than 4 or 5..??  I know it would be easier if my XML was is a different
> >format, but I'm forced to use the MS ADO export format.  Thank you,
> in
> >advance, for your help.
> 
> Hi Michael,
> 
> the problem here is something like this: your input isn't nested, and
> you
> want to nest it according to a LEVEL attribute.
> There are three problems with your approach.
> 
> 1. You're doing the same thing in 5 different templates. Try recursion
> instead, then you don't need to worry about how many levels you have.
> 
> 
> 2. The location paths in your <xsl:apply-templates>.
>  You use paths fine when you select attributes, so I assume you're
> using the
> full path (xml/rs:data/z:row) here because you want to get up out of
> your
> current context (a qualified z:row).
> To make your paths work, either use /xml/rs:data/z:row - the "/" will
> get
> you back to the document node, or use ../z:row[@LEVEL=2]- in longhand
> that's
> parent::node()/z:row[@LEVEL=2]. It's just like file directories.
> 
> BUT: it still won't work...
> 3. Matching children with parents.
> To get the nesting right you have to put the right children with the
> right
> parents. In your input you have CATEGORYID and PARENTCATEGORYID. A
> revised
> path, still using your approach would be
> 	"../z:row[(@LEVEL=2) and (@PARENTCATEGORYID =
> current()/@CATEGORYID)"
> or even 
> 	"../z:row[(@LEVEL=current()/@LEVEL + 1) and (@PARENTCATEGORYID
> =
> current()/@CATEGORYID)"
> You need current() not . because inside a predicate expression . is
> the node
> being evaluated, not the node matched by the current template.
> 
> Only recurse...
> Tom S-W
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 
> 

__________________________________________________
FREE voicemail, email, and fax...all in one place.
Sign Up Now! http://www.onebox.com


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords