[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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)
dynamic testing
Christof,

><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?
>	</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">
  <foo>
    <attribute name="width"><xsl:value-of select="$att" /></attribute>
  </foo>
</xsl:if>

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:

  test="string($att)"

instead.

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

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

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

Jeni Tennison
http://www.jenitennison.com/




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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.