You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Peter Carberry <mf...@gmail.com> on 2009/04/16 15:15:05 UTC

Can't access a variable's children in extension element

Hi all,

I'm having an issue with evaluating an XPath expression in an extension
element. It works fine for almost all cases I've tried so far, but if I have
a variable in my stylesheet which has child nodes under it, I'm having
difficulty when I try to resolve any of the children more than once. So for
a variable $blahVar which resolves to this element:

<entry code="blah">
   <firstamount>12345</firstamount>
   <secondamount>67890</secondamount>
</entry>

... I can pass my element something like <tst:myElement
value="$blahVar/firstamount" /> and have it work on '12345', however after
accessing the variable for the first time any subsequent accesses fail. If I
put that same call in again, or substitute $blahVar/secondamount then the
XPath variable always resolves to the empty string.

This is obviously possible - I have xsl:value-of doing the exact same
lookups with the same XPath strings right alongside and getting the correct
results. I've been checking through the xalan-j source this morning to see
if I could see what I'm missing, but I'm still quite new to xalan-j and
nothing has jumped out at me so far.

The code I'm using is as follows:

    protected static String xpathLookup(XSLProcessorContext context,
ElemExtensionCall call, String exp) throws TransformerException
    {
        String nodeText = "";

        if ((exp != null && exp.length() != 0))
        {
            TransformerImpl transformer = context.getTransformer();
            XPathContext xpContext = transformer.getXPathContext();

            try
            {
                xpContext.pushNamespaceContext(call);
                int current = xpContext.getCurrentNode();
                xpContext.pushCurrentNodeAndExpression(current,current);

                XPath dynamicXPath = new XPath(
                    exp,
                    xpContext.getSAXLocator(),
                    xpContext.getNamespaceContext(),
                    XPath.SELECT,
                    transformer.getErrorListener());

                Expression expression = dynamicXPath.getExpression();

                XObject xobj1 = expression.execute(xpContext);
                xpContext.popCurrentNodeAndExpression();
                xpContext.popNamespaceContext();
                nodeText = xobj1.toString();
            }
            catch(TransformerException te) {}
        }

        return nodeText;
    }

I know this has to be something really simple that I'm missing, any help is
appreciated.

Thanks,
Peter Carberry.

Re: Can't access a variable's children in extension element

Posted by Michael Ludwig <ml...@as-guides.com>.
Peter Carberry schrieb:
> Hi Michael,
>
> Sorry for the delay (been a busy week!), but here's a trivial example
> that exposes the issue. Run it and open the created file (result.html)
> in the browser to see the issue - the variable used can only be
> accessed once by the extension element, and so only the first amount
> is doubled in each case.

Hi Peter,

I can confirm the issue you're seeing (using Xalan 2.7.1), but I'm
sorry, I have no idea why this seems to work only the first time around.

Michael Ludwig

Re: Can't access a variable's children in extension element

Posted by Peter Carberry <mf...@gmail.com>.
Hi Michael,

Sorry for the delay (been a busy week!), but here's a trivial example that
exposes the issue. Run it and open the created file (result.html) in the
browser to see the issue - the variable used can only be accessed once by
the extension element, and so only the first amount is doubled in each case.

Peter.

2009/4/20 Michael Ludwig <mi...@gmx.de>

> Peter Carberry schrieb am 16.04.2009 um 15:03:03 (+0100):
> > Hi Michael,
> >
> > I'm not using any of the conversion functions, just creating a
> > standard XPath object and calling its execute method. I know from
> > debugging that the XObject returned is a nodeset, in the case of every
> > call after the first however it is empty.
> >
> > It seems as if whatever way I'm using XPath it's only allowing me to
> > resolve the variable the first time. If I replace all instances of the
> > variable in the stylesheet with the key statement that generates it
> > (ie switching "key('myKeyResolvesToBlah', 'blah')" for "$blahVar")
> > then the XPath queries all resolve OK. There's something that
> > <xsl:value-of select="" /> is doing that I am not, but I don't know
> > what it is.
>
> Hi Peter,
>
> maybe the best way to solve this is to post a small, self-contained
> example that exposes the problem, so others can reproduce it or debug it
> in their environments.
>
> Best,
>
> Michael
>

Re: Can't access a variable's children in extension element

Posted by Michael Ludwig <mi...@gmx.de>.
Peter Carberry schrieb am 16.04.2009 um 15:03:03 (+0100):
> Hi Michael,
> 
> I'm not using any of the conversion functions, just creating a
> standard XPath object and calling its execute method. I know from
> debugging that the XObject returned is a nodeset, in the case of every
> call after the first however it is empty.
> 
> It seems as if whatever way I'm using XPath it's only allowing me to
> resolve the variable the first time. If I replace all instances of the
> variable in the stylesheet with the key statement that generates it
> (ie switching "key('myKeyResolvesToBlah', 'blah')" for "$blahVar")
> then the XPath queries all resolve OK. There's something that
> <xsl:value-of select="" /> is doing that I am not, but I don't know
> what it is.

Hi Peter,

maybe the best way to solve this is to post a small, self-contained
example that exposes the problem, so others can reproduce it or debug it
in their environments.

Best,

Michael

Re: Can't access a variable's children in extension element

Posted by Peter Carberry <mf...@gmail.com>.
Hi Michael,

I'm not using any of the conversion functions, just creating a standard
XPath object and calling its execute method. I know from debugging that the
XObject returned is a nodeset, in the case of every call after the first
however it is empty.

It seems as if whatever way I'm using XPath it's only allowing me to resolve
the variable the first time. If I replace all instances of the variable in
the stylesheet with the key statement that generates it (ie switching
"key('myKeyResolvesToBlah', 'blah')" for "$blahVar") then the XPath queries
all resolve OK. There's something that <xsl:value-of select="" /> is doing
that I am not, but I don't know what it is.

Peter.

2009/4/16 Michael Ludwig <ml...@as-guides.com>

> Peter Carberry schrieb:
>
>>
>> I'm having an issue with evaluating an XPath expression in an
>> extension element. It works fine for almost all cases I've tried so
>> far, but if I have a variable in my stylesheet which has child nodes
>> under it, I'm having difficulty when I try to resolve any of the
>> children more than once.
>>
>
> Hi Peter,
>
> a non-repeatable read, so to say, in XSLT? That sounds curious.
>
>  So for a variable $blahVar which resolves to this element:
>>
>> <entry code="blah">
>>   <firstamount>12345</firstamount>
>>   <secondamount>67890</secondamount>
>> </entry>
>>
>> ... I can pass my element something like <tst:myElement
>> value="$blahVar/firstamount" />
>>
>
> Just to make sure: Does your variable evaluate to a node-set, because it
> is constructed using @select, or upgraded from a result tree fragment
> (RTF) to a node-set using one of the available conversion functions? You
> should get an error if not. On the other hand, I'm not sure in what way
> an extension element may  change the rules of the game.
>
> Michael Ludwig
>

Re: Can't access a variable's children in extension element

Posted by Michael Ludwig <ml...@as-guides.com>.
Peter Carberry schrieb:
>
> I'm having an issue with evaluating an XPath expression in an
> extension element. It works fine for almost all cases I've tried so
> far, but if I have a variable in my stylesheet which has child nodes
> under it, I'm having difficulty when I try to resolve any of the
> children more than once.

Hi Peter,

a non-repeatable read, so to say, in XSLT? That sounds curious.

> So for a variable $blahVar which resolves to this element:
>
> <entry code="blah">
>    <firstamount>12345</firstamount>
>    <secondamount>67890</secondamount>
> </entry>
>
> ... I can pass my element something like <tst:myElement
> value="$blahVar/firstamount" />

Just to make sure: Does your variable evaluate to a node-set, because it
is constructed using @select, or upgraded from a result tree fragment
(RTF) to a node-set using one of the available conversion functions? You
should get an error if not. On the other hand, I'm not sure in what way
an extension element may  change the rules of the game.

Michael Ludwig