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

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

>Hi,
>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:

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

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

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

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