Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ operator ?: in xpath?

3 views
Skip to first unread message

bar...@bluezone.no

unread,
Oct 26, 2007, 7:22:29 AM10/26/07
to
Hi,

is there an Xpath equivalent to the ternary conditional operator ?: in
C++ and other languages?
I'd like to do xsl:sort where the select should use one value of it
exists, otherwise another.
Something like <xsl:sort select="$node/@tag1 ? $node/@tag1 : $node/
@tag2" />

Thanks!

Julian F. Reschke

unread,
Oct 26, 2007, 7:41:39 AM10/26/07
to


You should be able to just do:

<xsl:sort select="$node/@tag1" />
<xsl:sort select="$node/@tag2" />

Best regards, Julian

bar...@bluezone.no

unread,
Oct 26, 2007, 7:57:19 AM10/26/07
to
On 26 Okt, 13:41, "Julian F. Reschke" <julian.resc...@nospam-

greenbytes.de> wrote:
> bar...@bluezone.no wrote:
> > Hi,
>
> > is there an Xpath equivalent to theternaryconditional operator ?: in

> > C++ and other languages?
> > I'd like to do xsl:sort where the select should use one value of it
> > exists, otherwise another.
> > Something like <xsl:sort select="$node/@tag1 ? $node/@tag1 : $node/
> > @tag2" />
>
> > Thanks!
>
> You should be able to just do:
>
> <xsl:sort select="$node/@tag1" />
> <xsl:sort select="$node/@tag2" />
>
> Best regards, Julian

Thanks for quick reply!
But doesn't that mean that I will sort on tag1 for all nodes first,
then tag2?
I would like to sort on tag2 only for the nodes that do not have tag1.
This is a very special case, I know, but it may happen in my
application...

Julian F. Reschke

unread,
Oct 26, 2007, 9:18:20 AM10/26/07
to

What difference does this make for the result?

BR, Julian

bar...@bluezone.no

unread,
Oct 26, 2007, 9:55:22 AM10/26/07
to
On 26 Okt, 15:18, "Julian F. Reschke" <julian.resc...@nospam-
> BR, Julian- Skjul sitert tekst -
>
> - Vis sitert tekst -

Well, let's say node1 has no tag1, but it's tag2 is greater than
node2's @tag1.
Wouldn't


<xsl:sort select="$node/@tag1" />
<xsl:sort select="$node/@tag2" />

give node2 before node1 (if sort is descending)?
(I want node1 before node2.)

But I am not sure what two <xsl:sort> after each other really mean. I
am grateful for the help!

Julian F. Reschke

unread,
Oct 26, 2007, 11:10:28 AM10/26/07
to
bar...@bluezone.no wrote:
> Well, let's say node1 has no tag1, but it's tag2 is greater than
> node2's @tag1.
> Wouldn't
> <xsl:sort select="$node/@tag1" />
> <xsl:sort select="$node/@tag2" />
> give node2 before node1 (if sort is descending)?
> (I want node1 before node2.)
>
> But I am not sure what two <xsl:sort> after each other really mean. I
> am grateful for the help!

I see. Yes, that's different.

Two sorts just mean that the second one is applied when the first
evaluates to equal.

You can probably achieve what you want by playing with string
operations, such as

<xsl:sort select="concat($node/@tag1,$node/@tag2)" />

Best regards, Julian

Dimitre Novatchev

unread,
Oct 27, 2007, 1:32:10 AM10/27/07
to

<bar...@bluezone.no> wrote in message
news:1193397749.3...@o38g2000hse.googlegroups.com...


Use:
<xsl:sort select="$node/@tag1 | $node[not(@tag1)]"/>


In the above expression the two operands of the union operator are mutually
exclusive, that is, the union will always consist of just one of the two
operands.


Cheers,
Dimitre Novatchev


bar...@bluezone.no

unread,
Oct 27, 2007, 11:50:53 AM10/27/07
to

Hi,

I'm not sure if I follow you...
I can have nodes with both @tag1 and @tag2, and then I want to sort on
@tag1's values.
Then I have nodes with just @tag2, and then I want to sort on it's
value.
(I always have @tag2.)

So the ?: operator in C++ is exactly what I need in Xpath. Or an if-
then-else.

Anthony Jones

unread,
Oct 27, 2007, 5:06:43 PM10/27/07
to

"Dimitre Novatchev" <dimi...@tpg.com.au> wrote in message
news:4722cd5b$0$47116$892e...@authen.yellow.readfreenews.net...


>
> <bar...@bluezone.no> wrote in message
> news:1193397749.3...@o38g2000hse.googlegroups.com...
> > Hi,
> >
> > is there an Xpath equivalent to the ternary conditional operator ?: in
> > C++ and other languages?
> > I'd like to do xsl:sort where the select should use one value of it
> > exists, otherwise another.
> > Something like <xsl:sort select="$node/@tag1 ? $node/@tag1 : $node/
> > @tag2" />
>
>
> Use:
> <xsl:sort select="$node/@tag1 | $node[not(@tag1)]"/>
>

Shouldn't this be:-

<xsl:sort select="$node/@tag1 | $node[not(@tag1)]/@tag2"/>


--
Anthony Jones - MVP ASP/ASP.NET


Dimitre Novatchev

unread,
Nov 1, 2007, 1:31:20 AM11/1/07
to

"Anthony Jones" <A...@yadayadayada.com> wrote in message
news:ekAII1NG...@TK2MSFTNGP02.phx.gbl...

Correct. I didn't see the @tag2 continuing on the last line.


Cheers,
Dimitre Novatchev.


Dimitre Novatchev

unread,
Nov 1, 2007, 1:38:48 AM11/1/07
to

<bar...@bluezone.no> wrote in message
news:1193500253.0...@22g2000hsm.googlegroups.com...

As Anthony Jones noticed, I missed completely that you wanted to use @tag2
in the sorting.

In this case the XPath expression that produces the sort keys is:

<xsl:sort select="$node/@tag1 | $node[not(@tag1)]/@tag2"/>


And this is exactly what you said you wanted. The first operand will
evaluate to (non-empty node-set of an) "tag1" attribute in all cases when
$node has a "tag1" attribute. The second operand may be a (non-empty
node-set of an) "tag2" attribute only if $node does not have a "tag1"
attribute.


Cheers,
Dimitre Novatchev


0 new messages