You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by "Schmitt, Christian" <Ch...@Dresdner-Bank.com> on 2000/11/30 14:23:54 UTC
[C2] xsp-attribute behaviour
Hi all,
please consider the following XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsp:page language="java"
xmlns:xsp="http://apache.org/xsp">
<my-root>
<xsp:attribute name="attr">
<xsp:expr>new String("something").toString()</xsp:expr>
</xsp:attribute>
<element>
Should have no attribute
</element>
</my-root>
</xsp:page>
I was expecting something like this as output:
<?xml version="1.0"?>
<my-root attr="something">
<element>
Should have no attribute
</element>
</my-root>
Instead I'm getting the attribute in <element> and <my-root> has no
attribute.
Here's a snippet from the generated source code:
public void generate() throws SAXException, ProcessingException {
try {
this.contentHandler.startDocument();
AttributesImpl xspAttr = new AttributesImpl();
this.contentHandler.startPrefixMapping("xsp",
"http://apache.org/xsp");
this.contentHandler.startElement("", "my-root", "my-root", xspAttr);
xspAttr.clear();
this.characters("\n ");
xspAttr.addAttribute("", "attr", "attr", "CDATA",
String.valueOf(new
String("something").toString()) + "");
this.characters("\n ");
this.contentHandler.startPrefixMapping("xsp",
"http://apache.org/xsp");
this.contentHandler.startElement("", "element", "element", xspAttr);
xspAttr.clear();
this.characters("\n Should have no attribute\n ");
this.contentHandler.endElement("", "element", "element");
this.contentHandler.endPrefixMapping("xsp");
this.characters("\n ");
this.contentHandler.endElement("", "my-root", "my-root");
this.contentHandler.endPrefixMapping("xsp");
this.contentHandler.endDocument();
} catch (Throwable t) {
cocoonLogger.debug("xsp.xsl: handled developer exception: ", t);
throw new ProcessingException(t.getMessage());
}
}
Re: [C2] xsp-attribute behaviour
Posted by Paul Russell <pa...@luminas.co.uk>.
On Fri, Dec 01, 2000 at 04:47:36PM +0100, Marcus Crafter wrote:
> Example:
>
> <xsp:element name="tag">
>
> <xsp:logic>
> String var = myclass.getVar();
> </xsp:logic>
>
> <xsp:attribute name="attr1">
> <xsp:expr>var</xsp:expr>
> </xsp:attribute>
>
> </xsp:element>
I'd have *thought* that should be:
<xsp:element name="tag">
<xsp:logic>
String var = myclass.getVar();
<xsp:attribute name="attr1">
<xsp:expr>var</xsp:expr>
</xsp:attribute>
</xsp:logic>
</xsp:element>
I'm no XSP expert (yet) though, so I'm not *sure*. does that work?
P.
--
Paul Russell <pa...@luminas.co.uk>
Technical Director, http://www.luminas.co.uk
Luminas Ltd.
Re: [C2] xsp-attribute behaviour
Posted by Marcus Crafter <cr...@dresdnerbank.de>.
Hi Christian,
Interesting stuff we're seeing here.
Just for everyone else's information. Christian and I played around
with the xsp.xsl file for a while and we noticed some interesting
behaviour. It's kind of strange, perhaps some one has an idea how to
solve these issues ?
First issue is that xsp:attribute templates are evaluated after the
call to 'startElement', in the generic 'handle-all-tags-except-xsp-tags'
template (xsp.xsl, line 327).
This means child xsp:attribute tags aren't actually associated with
their parent - they associate to the next tag (is this a bug or
intended ?)
xsp:element does things differently though (I would say, more
correctly), it evaluates xsp:attribute templates first, generates the
call to 'startElement', and then evaluates all other templates
(xsp.xsl, line 127) - but this raises a second issue ->
If one uses xsp:logic to create/define a variable, and then uses
xsp:expr within xsp:attribute then there are no guarantees that the
variable will actually exist when the xsp:expr is executed.
Example:
<xsp:element name="tag">
<xsp:logic>
String var = myclass.getVar();
</xsp:logic>
<xsp:attribute name="attr1">
<xsp:expr>var</xsp:expr>
</xsp:attribute>
</xsp:element>
Looks ok, but it isn't. The xsp:element template evaluates first
xsp:attribute tags, then it creates the call to 'startElement', and
then it evaluates all other templates (which includes xsp:logic).
The above example throws an exception because the variable 'var' isn't
defined - ie. The use of the variable 'var' (xsp:attribute) occurs in
the generated java code before its definition (xsp:logic).
The fix to this problem - not sure. We could evaluate xsp:logic
tags before xsp:attribute to ensure that any developer defined
variables exist - however that causes all sorts of bother if the user
has called SAX methods directly in their xsp:logic tags (we don't
here, but perhaps others have ?).
Granted, in this example you could do everything in one line between
xsp:expr, but with more complicated code - one-liners aren't as
pretty.
Any thoughts ?
Cheers,
Marcus
On Thu, 30 Nov 2000, Schmitt, Christian wrote:
> Hi all,
> please consider the following XML:
>
> <?xml version="1.0" encoding="iso-8859-1"?>
> <xsp:page language="java"
> xmlns:xsp="http://apache.org/xsp">
> <my-root>
> <xsp:attribute name="attr">
> <xsp:expr>new String("something").toString()</xsp:expr>
> </xsp:attribute>
> <element>
> Should have no attribute
> </element>
> </my-root>
> </xsp:page>
>
> I was expecting something like this as output:
>
> <?xml version="1.0"?>
> <my-root attr="something">
> <element>
> Should have no attribute
> </element>
> </my-root>
>
> Instead I'm getting the attribute in <element> and <my-root> has no
> attribute.
> Here's a snippet from the generated source code:
> public void generate() throws SAXException, ProcessingException {
> try {
> this.contentHandler.startDocument();
> AttributesImpl xspAttr = new AttributesImpl();
> this.contentHandler.startPrefixMapping("xsp",
> "http://apache.org/xsp");
> this.contentHandler.startElement("", "my-root", "my-root", xspAttr);
> xspAttr.clear();
> this.characters("\n ");
> xspAttr.addAttribute("", "attr", "attr", "CDATA",
> String.valueOf(new
> String("something").toString()) + "");
> this.characters("\n ");
> this.contentHandler.startPrefixMapping("xsp",
> "http://apache.org/xsp");
> this.contentHandler.startElement("", "element", "element", xspAttr);
> xspAttr.clear();
> this.characters("\n Should have no attribute\n ");
> this.contentHandler.endElement("", "element", "element");
> this.contentHandler.endPrefixMapping("xsp");
> this.characters("\n ");
> this.contentHandler.endElement("", "my-root", "my-root");
> this.contentHandler.endPrefixMapping("xsp");
> this.contentHandler.endDocument();
> } catch (Throwable t) {
> cocoonLogger.debug("xsp.xsl: handled developer exception: ", t);
> throw new ProcessingException(t.getMessage());
> }
> }
>
> >From the code I can see that <my-root> is being started with an empty
> xspAttr object. Only then are the attributes added to the xspAttr object and
> then <element> is started (with a now not-empty xspAttr object).
>
> Is this expected behaviour of the <xsp:attribute> element?
>
> Thanks for any insight,
> Christian Schmitt
--
.....
,,$$$$$$$$$, Marcus Crafter
;$' '$$$$: Computer Systems Engineer
$: $$$$: Open Software Associates GmbH
$ o_)$$$: 82-84 Mainzer Landstrasse
;$, _/\ &&:' 60327 Frankfurt Germany
' /( &&&
\_&&&&' Email : Marcus.Crafter@osa.de
&&&&. Business Hours : +49 69 9757 200
&&&&&&&: After Hours : +49 69 49086750