RE: [xsl] pointer to a node/nodeset?

Subject: RE: [xsl] pointer to a node/nodeset?
From: "Hunsberger, Peter" <Peter.Hunsberger@xxxxxxxxxx>
Date: Fri, 16 Aug 2002 09:28:43 -0500
> Basically, I have a xml structure like this:
> 
> <resultset>
>
>  <metadata>
>    <path name="DONOR">   <!--  #1 -->
>      <property name="label">Donor</property>
>      <path name="DONOR_ID">
>        <property name="label">Donor Id</property>
>        <property name="link">DONOR</property>
>      </path>
>      <path name="DOCUMENT">
>        <property name="label">Document</property>
>        <path name="URL"/>
>      </path>
>    </path>
>  </metadata>
>
>  <result>
>    <result-item name="DONOR">
>      <attr name="DONOR_ID">100</attr>   <!-- #2 -->
>      <ref name="DOCUMENT">
>        <attr name="URL"></attr>
>      </ref>
>    </result-item>
>  </result>
>
> </resultset>
>
[snip]

> This would be a lot more efficient if I moved through the metadata at the
> same time as the result.. is there a way to maintain a pointer to the
> appropriate metadata node [snip] and pass it around with xsl:param while
I'm traversing
> the <result>?

Yes, just create the variable higher up in the tree and pass it along as a
parameter to the templates:

	<xsl:variable name="meta" select="metadata/*"/>		<!-- adjust
context as needed -->
	
	...	

	<xsl:apply-temaplates select="result">	<!-- or whatever -->
		<xsl:with-param name="metanode" select="$meta"/>
	</xsl:apply-templates>

	...

	<xsl:template match="result">			<!-- or whatever -->
		<xsl:param name="metanode"/>

		...

		<xsl:variable name="reference" select="$meta/*[@name =
'DONOR_ID']">


> So I'm at node #2 I'd have a variable $metanode that points to #1 somehow.
> Then I'd know to go to its child with name "DONOR_ID",  and that would be
> the next $metanode.

Once you have the parameter you can pass just portions of the meta data on,
perhaps recursively:

	<xsl:apply-templates select="*">
      	<xsl:with-param name="metanode" select="$reference"/>
	</xsl:apply-templates>

> Or can anyone see an even nicer way of doing this, with keys or something
> fancy?  :)

Keys will also work, but the require that the context of the metadata be
accessible at the time you use the key. This is sometimes hard to arrange
and if you want to refer to arbitrary nodes efficiently you may end up with
something like:

	<xsl:key name="metadata"  match="/metadata/* | /metadata/*/* |
/metadata/*/*/* " use="@name"/>

This does however avoid having to pass the parameters through many levels of
style sheet, since you may now be able to refer to the meta data directly
using something like:

	select=key('metadata',current()/@name)

where you are on a node that has the correct "name" attribute.  I once asked
about the efficiency of using keys in this manner versus using variables and
basically got told to test it both ways.   That's something I haven't
managed to be able to do yet, so I can't tell you whether either pattern is
generally going to be more efficient...




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


Current Thread