Java 5/JAXP/ setParameter avoid conversion to string

P

Piet71

Hi,
I have a stylesheet to select nodes from a source document via their
"position" attribute. The template snippet is as follows:
<xsl:template match="counter">
<xsl:if test="$number"><!--@pos=2 or @pos=5 or @pos=8 or @pos=11 or
@pos=13 or @pos=15-->
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>

$number is a parameter supplied by the user. An example is given in
the commented section. This is running nicely from the command line
with Xalan 2.7.
However when I try to set this parameter from within my Java code
(Java 1.5).
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setParameter("number","@pos=1 or @pos=2");
apparently what is supposed to be an XPath expression is treated as a
simple string which is non-empty and thus evaluated in a boolean
context as true. Consequently, instead of getting just the counters
with the appropriate positions, I get all counters back!
I had a brief look in the source code of
org.apache.xalan.xslt.Process.java which is the class that does the
transformation from the command line, and their as well all parameters
are treated as Strings (they are even explictly casted).
So my question is why are the two ways giving different results? How
can I avoid that "@pos=1 or @pos=2" is transformed from an expression
to a simple string? Is their a special property that I have to set?
Many thanks for your help!
Best regards
Piet
 
J

Joe Kesselman

Piet71 said:
How can I avoid that "@pos=1 or @pos=2" is transformed from an expression
to a simple string?

In general you can't.

In *SOME* cases, you can use an Attribute Value Template to have an
expression evaluated to produce the value for an attribute (see the XSLT
spec for a discussion of how that works), but I suspect this isn't one
of them.

Some XSLT processors -- not all by a long shot -- support an extension
function which can take an expression as a string and evaluate it in the
current context.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top