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 David Portabella Clotet <da...@gmail.com> on 2006/01/18 18:39:21 UTC
using a function inside a select
Hello,
If I run the compact simple example provided at the end, I get this result.
<test>
<a>Hello</a>
<b/>
<c>World</c>
</test>
For 'a' and 'c' it works as expected, but I do not understand the
behaviour of 'b'.
For the 'a' case, the select computes the validity of t1/t2[1] and as
it is not satisfied, it looks for the next one, t1/t2[2].
Instead, for the 'b' case, the select only computes the validity of
t1/t2[1], and although it is not satisfied, it does not continue with
the next one.
Why this?
because of using a function (normalize-space)?
Is this a bug (I am using org.apache.xalan.xslt.Process)
or a property of the XSLT language?
If it is a property, how to achieve my expected behaviour?
-----
I need to use the normalize-space function so that the stylesheet
works in cases like this:
<test>
<t1>
<t2>Hello</t2>
<t2>
World</t2>
</t1>
</test>
-----
=========
xml file:
<test>
<t1>
<t2>Hello</t2>
<t2>World</t2>
</t1>
</test>
stylesheet:
<xsl:template match="/test">
<test>
<a><xsl:value-of select="t1[t2='World']/t2"/></a>
<b><xsl:value-of select="t1[normalize-space(t2)='World']/t2"/></b>
<c><xsl:value-of select="t1/t2[.='World']"/></c>
</test>
</xsl:template>
Re: using a function inside a select
Posted by David Portabella Clotet <da...@portabella.name>.
Hello,
> <d><xsl:value-of select="t1[t2[normalize-space(.)='World']]/t2"/></d>
I see. That's exactly what I was looking for.
Many thanks,
DAvid
On 3/14/06, Henry Zongaro <zo...@ca.ibm.com> wrote:
> Hi, David.
>
> David Portabella Clotet <da...@gmail.com> wrote on 2006-01-18
> 12:39:21 PM:
> > If I run the compact simple example provided at the end, I get this
> result.
> >
> > <test>
> > <a>Hello</a>
> > <b/>
> > <c>World</c>
> > </test>
> >
> > For 'a' and 'c' it works as expected, but I do not understand the
> > behaviour of 'b'.
> ...
> > xml file:
> > <test>
> > <t1>
> > <t2>Hello</t2>
> > <t2>World</t2>
> > </t1>
> > </test>
> >
> > stylesheet:
> > <xsl:template match="/test">
> > <test>
> > <a><xsl:value-of select="t1[t2='World']/t2"/></a>
> > <b><xsl:value-of select="t1[normalize-space(t2)='World']/t2"/></b>
> > <c><xsl:value-of select="t1/t2[.='World']"/></c>
> > </test>
> > </xsl:template>
>
> I don't know whether anybody ever responded to your question, so I
> hope you figured out the problem a long time ago.
>
> In any event, for the expression, t1[t2='World']/t2 the expression t2
> in the predicate results in a node-set containing the two child element
> nodes of t1. According to section 3.4 of XPath,[1] if one object in
> relational or equality expression is a node-set and the other is a string,
> the result of the expression is true if and only if there is a node for
> which the expression is true. The comparison is true for the second t2
> node, so the predicate is true.
>
> For the expression t1[normalize-space(t2)='World']/t2, the expected
> argument type for normalize-space is "string?". According to section 3.2
> of XPath,[2] an argument to a function will be converted to the expected
> type - for string, the conversion is performed as if by the string
> function.[3] Given a node-set as its argument, the string function
> returns the string value of the node that is first in document order. In
> this case, the argument to normalize-space is effectively the string value
> of the first t2 node, "Hello", so the value of the predicate is false.
>
> This might be what you're looking for, though I can't be certain,
> since you didn't actually describe what result you needed.
>
> <d><xsl:value-of select="t1[t2[normalize-space(.)='World']]/t2"/></d>
>
> I hope that helps.
>
> Thanks,
>
> Henry
> [1] http://www.w3.org/TR/xpath#booleans
> [2] http://www.w3.org/TR/xpath#section-Function-Calls
> [3] http://www.w3.org/TR/xpath#function-string
> ------------------------------------------------------------------
> Henry Zongaro Xalan development
> IBM SWS Toronto Lab T/L 969-6044; Phone +1 905 413-6044
> mailto:zongaro@ca.ibm.com
Re: using a function inside a select
Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, David.
David Portabella Clotet <da...@gmail.com> wrote on 2006-01-18
12:39:21 PM:
> If I run the compact simple example provided at the end, I get this
result.
>
> <test>
> <a>Hello</a>
> <b/>
> <c>World</c>
> </test>
>
> For 'a' and 'c' it works as expected, but I do not understand the
> behaviour of 'b'.
...
> xml file:
> <test>
> <t1>
> <t2>Hello</t2>
> <t2>World</t2>
> </t1>
> </test>
>
> stylesheet:
> <xsl:template match="/test">
> <test>
> <a><xsl:value-of select="t1[t2='World']/t2"/></a>
> <b><xsl:value-of select="t1[normalize-space(t2)='World']/t2"/></b>
> <c><xsl:value-of select="t1/t2[.='World']"/></c>
> </test>
> </xsl:template>
I don't know whether anybody ever responded to your question, so I
hope you figured out the problem a long time ago.
In any event, for the expression, t1[t2='World']/t2 the expression t2
in the predicate results in a node-set containing the two child element
nodes of t1. According to section 3.4 of XPath,[1] if one object in
relational or equality expression is a node-set and the other is a string,
the result of the expression is true if and only if there is a node for
which the expression is true. The comparison is true for the second t2
node, so the predicate is true.
For the expression t1[normalize-space(t2)='World']/t2, the expected
argument type for normalize-space is "string?". According to section 3.2
of XPath,[2] an argument to a function will be converted to the expected
type - for string, the conversion is performed as if by the string
function.[3] Given a node-set as its argument, the string function
returns the string value of the node that is first in document order. In
this case, the argument to normalize-space is effectively the string value
of the first t2 node, "Hello", so the value of the predicate is false.
This might be what you're looking for, though I can't be certain,
since you didn't actually describe what result you needed.
<d><xsl:value-of select="t1[t2[normalize-space(.)='World']]/t2"/></d>
I hope that helps.
Thanks,
Henry
[1] http://www.w3.org/TR/xpath#booleans
[2] http://www.w3.org/TR/xpath#section-Function-Calls
[3] http://www.w3.org/TR/xpath#function-string
------------------------------------------------------------------
Henry Zongaro Xalan development
IBM SWS Toronto Lab T/L 969-6044; Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com