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 Simon Kitching <si...@ecnetwork.co.nz> on 2002/06/11 06:19:46 UTC

Evaluating xpath expression in extension element

Hi All,
 
I am trying to implement an extension element like this:
  <ext:field select="..."  myattr1="..." myattr2="..."/>
where the select should be able to contain anything that would normally
be valid
in an <xsl:value-of select="..."/> expression. 
 
I have got this mostly working, but my code fails for this type of
thing:
 
<xsl:variable name="foo" select="'some value'"/>
<ext:field select="$foo"/>
 
My code which evaluates the specified select statement reports that the
variable 
"foo" cannot be found, though XPath expressions are evaluated correctly
(as far as I can see). Variable "foo" is definitely in scope; replacing
a call to my
extension with code like <xsl:value-of select="$foo"/> works fine.
 
Within my extension element method, I use the following code (mostly
grabbed
from the PipeDocument code):
 
XPathContext xpContext = procContext.getTransformer().getXPathContext();
XPath dynamicXPath = new XPath(
  selectStr, 
  xpContext.getSAXLocator(),
  xpContext.getNamespaceContext(),
  XPath.SELECT,
  procContext.getTransformer().getErrorListener());
 
XObject xobj = dynamicXPath.execute(
  xpContext,
  procContext.getContextNode(), // I suspect this is the problem
  xpContext.getNamespaceContext());
  
Note that global variables can be accessed from the select expression,
just not 
variables declared in the same template as the extension element
invocation.
 
My suspicion (which may be wildly wrong) is that the node returned by
procContext.getContextNode()
is the template node, not the <ext:field> node. And of course any
variables declared
within that node are therefore not in scope.
 
Does anyone have any suggestions how to deal with this?
I cannot find any methods that would return the <ext:field> node itself,
and am not sure
that if I did find one then xpath expressions might not be right if I
used it. Note also that
the select expression might be *any* valid expression.
 
On a related question, I am somewhat confused about what node
getContextNode()
is returning anyway :-)
 
I am assuming the following:
* a DOM (or DOM-equivalent) has been built for the source xml document
* a DTM tree of "wrapper" nodes is built; each node has a reference back
to the
   underlying source document node. It is to this DTM tree that any XSL
variables are
   attached.
* the getContextNode() method returns a reference to the DTM node which
wraps
  the currently matched source document node.
 
Could someone kindly correct me if I am off-beam here? Or maybe refer me
to
an appropriate architectural document?
 
Many thanks in advance,
 
Simon

Re: Evaluating xpath expression in extension element

Posted by "Frank E. Weiss" <fr...@well.com>.
Have you tried using an attribute value template (7.6.2) as follows?

<ext:field select="{...}" .../>

The curly braces are commonly used to evaluate XPath within attributes of literal result elements such as:

<img src="{...}"/>

Note that the expression is converted to string, just as with <xsl:value-of/>. If you want to use node sets or if you
need to evaluate the expression in a custom context, then using  attribute value template may not work.

-- Frank Weiss

Simon Kitching wrote:

>  Hi All,I am trying to implement an extension element like this:  <ext:field select="..."  myattr1="..."
> myattr2="..."/>where the select should be able to contain anything that would normally be validin an <xsl:value-of
> select="..."/> expression. I have got this mostly working, but my code fails for this type of thing:<xsl:variable
> name="foo" select="'some value'"/><ext:field select="$foo"/>My code which evaluates the specified select statement
> reports that the variable "foo" cannot be found, though XPath expressions are evaluated correctly(as far as I can
> see). Variable "foo" is definitely in scope; replacing a call to myextension with code like <xsl:value-of
> select="$foo"/> works fine.Within my extension element method, I use the following code (mostly grabbedfrom the
> PipeDocument code):XPathContext xpContext = procContext.getTransformer().getXPathContext();XPath dynamicXPath = new
> XPath(  selectStr,   xpContext.getSAXLocator(),  xpContext.getNamespaceContext(),  XPath.SELECT,
> procContext.getTransformer().getErrorListener());XObject xobj = dynamicXPath.execute(  xpContext,
> procContext.getContextNode(), // I suspect this is the problem  xpContext.getNamespaceContext());Note that global
> variables can be accessed from the select expression, just not variables declared in the same template as the
> extension element invocation.My suspicion (which may be wildly wrong) is that the node returned by
> procContext.getContextNode()is the template node, not the <ext:field> node. And of course any variables declaredwithin
> that node are therefore not in scope.Does anyone have any suggestions how to deal with this?I cannot find any methods
> that would return the <ext:field> node itself, and am not surethat if I did find one then xpath expressions might not
> be right if I used it. Note also thatthe select expression might be *any* valid expression.On a related question, I am
> somewhat confused about what node getContextNode()is returning anyway :-)I am assuming the following:* a DOM (or
> DOM-equivalent) has been built for the source xml document* a DTM tree of "wrapper" nodes is built; each node has a
> reference back to the   underlying source document node. It is to this DTM tree that any XSL variables are
> attached.* the getContextNode() method returns a reference to the DTM node which wraps  the currently matched source
> document node.Could someone kindly correct me if I am off-beam here? Or maybe refer me toan appropriate architectural
> document?Many thanks in advance,Simon