[xsl] Dynamic attribute selection

Subject: [xsl] Dynamic attribute selection
From: "Alex Cole" <a.cole@xxxxxxxxxxxxxxx>
Date: Fri, 5 Sep 2008 14:19:50 +0100
Hello all

I am trying to create a template to select values from a node based on
whether or not that node contains a certain attribute, for example:

<p>
	<c x="" y="27" />
	<c z="" y="67" />
</p>

<xsl:value-of select="p/c[@x]/@y]" />

That will display 27 as expected and required, and this is exactly the
effect I want, however I am having trouble porting it to a template where
you can specify as a parameter the name of the attribute you want to ensure
is present.  The code I currently have looks something like:

<p>
	<c x="" y="27" />
	<c z="" y="67" />
</p>

<xsl:call-template name="MY_TEMPLATE"><xsl:with-param name="t" select="'x'"
/></xsl:call-template>

<xsl:template name="MY_TEMPLATE">
	<xsl:param name="t" />
	<xsl:value-of select="p/c[$t]/@y]" />
</xsl:template>

However that obviously doesn't work.  I've tried all sorts of methods
including using <xsl:key name="ATTR_NAME" match="@*" use="name()"/> to check
it, but it didn't like using that with the additional path parts.  Looping
though all c's to try test the existence of that using <xsl:if test="$t">,
but that just seems to evaluate the string, not the value of the attribute
represented by the string and I'm sure a few other methods I've lost track
of.  I've been trying to get this to work for quite a while now, I've found
a few useful things round and about, but nothing that works.

I am using the default javax 1.5 XSL library on eclipse 3.4

Any assistance in this matter would be greatly appreciated, I'm beginning to
lose hair over this one.

Thank you in advance

Alex Cole

Current Thread