Re: dynamic testing of an attribute

Subject: Re: dynamic testing of an attribute
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 30 Oct 2000 11:46:43 -0400 (EST)

><xsl:if test="./@*[name()=$attname]">    //does not work!

You con't say *how* it doesn't work, but it's probably to do with using the
shorthand . in a test, which is not allowed.  The intent of your test is:

  test="@*[name() = $attname]"

so try that instead.

>	<attribute name="width">
>		<xsl:value-of select="???"/>   //how to specify the value of the
>	</attribute>

Presumably you want the value of the $attname attribute?  The value of an
attribute is given as its string value; the xsl:value-of instruction gives
the string value of whatever XPath is specified within the select
expression.  So if you want the value of an attribute within the
xsl:value-of, then use an XPath that points to that attribute:

  <xsl:value-of select="@*[name() = $attname]" />

If this is what you wanted, then to prevent the same attribute being
searched for twice (once in the xsl:if test, and once in the xsl:value-of
select), then you could assign the attribute to a variable and query that:

<xsl:variable name="att" select="@*[name() = $attname]" />
<xsl:if test="$att">
    <attribute name="width"><xsl:value-of select="$att" /></attribute>

A couple of things to note here.  One is that the xsl:if tests whether the
attribute is *present* rather than whether the attribute has a value.  In
other words, if $attname is 'bar', then:

  <foo bar="" />

would have the test return true.  If you want to test whether the attribute
is present *and* has a value, then use:



The second thing is that I just wanted to check that you really wanted to
create output that looked like:

    <attribute name="width">value</attribute>

or whether you were actually after:

  <foo width="value" />

If the latter, then you need to prefix your 'attribute' elements with xsl:
to put them in the XSLT namespace and have the XSLT processor recognise
them as instructions.  Alternatively, you can use an attribute value
template to achieve the same effect:

  <foo width="{$att}" />

I hope that this helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread