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

Re: using variable as a value for "use-attribute-sets

Subject: Re: using variable as a value for "use-attribute-sets"
From: Trevor Nash <tcn@xxxxxxxxxxxxx>
Date: Sat, 05 Jan 2002 14:03:52 +0000
using variables in stylesheet
On Sat, 5 Jan 2002 16:33:44 +0530, you wrote:

>I have a variable declared called "setname"
>I want to use it as the value of  "use-attribute-sets".

use-attribute -sets will not work, because the name has to be known
when the stylesheet is compiled (just like the names of variables,
names of templates etc).

Your options really boil down to two basic themes, depending on
whether your attribute names and values are constant or depend on some
way on the input stylesheet.

If the names and values are constant, then the easy thing to do is
make a document containing prototype elements like:

    <block name="me" foo="bar" />
    <other name="you" bar="foo" />

If you put that in a file named "attributes.xml" then in your template
you can say:

      <xsl:copy-of select="document('attributes.xml')
                /*/*[local-name()='block']/@*" />

The copy-of makes copies of the attribute nodes in the 'look up'
document, selecting on the name of the element.  The first * matches
the 'prototypes' element.  I use local-name() to avoid future problems
if you introduce namespaces.  If the element name is variable, then go
back to using xsl:element as in your original message.

But what happens if the attribute names or values depend somehow on
the input document?  You need to do two passes.  The simple approach
is to write another stylesheet which just creates an appropriate
attributes.xml.  You then need to run two stylesheets: if you are
doing this client side that involves a bit of Javascript.

If you want a one pass self-contained stylesheet, then you have to use
the node-set extension function - look it up in the documentation for
your particular processor.  The approach is the same, but instead of
putting your prototype attributes in a file, they go in a global
variable in the stylesheet.  Then you replace the document() call with
xx:node-set($attributes)  -- or whatever you decide to call the

If you need attribute sets referring to other attribute sets, that can
be done as well: but I won't go into that unless you need it.  It has
been discussed on this list before under 'inheritance'.  It is simple
in concept - just nest elements in your look-up file - but you might
find the details tricky if you want to mimic the full behaviour of
attribute sets.

If the attributes are constant, then instead of a separate file you
can embed the table in the stylesheet itself - you should find that in
the FAQ, something like 'look up tables' I think.  If you have Mike
Kay's book, its on page 474.  If you don't, get one!

Hope this helps,
Trevor Nash
Traditional training & distance learning,
Consultancy by email

Melvaig Software Engineering Limited
voice:     +44 (0) 1445 771 271 
email:     tcn@xxxxxxxxxxxxx

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

Current Thread


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.
First Name
Last Name
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.