From: Daniel Pitts on
I have a document which has two elements: ValueOverride and ValueDefault
For example:
<MyDoc>
<ValueOverride>The override value</ValueOverride>
<ValueDefault>The default value</ValueDefault>
</MyDoc>

I need a single XPath expression that will return ValueOverride if it is
non-empty, and ValueDefault otherwise.

Is this possible to do with XPath?

Thanks,
Daniel.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Daniel Pitts on
On 3/12/2010 2:56 PM, Daniel Pitts wrote:
> I have a document which has two elements: ValueOverride and ValueDefault
> For example:
> <MyDoc>
> <ValueOverride>The override value</ValueOverride>
> <ValueDefault>The default value</ValueDefault>
> </MyDoc>
>
> I need a single XPath expression that will return ValueOverride if it is
> non-empty, and ValueDefault otherwise.
>
> Is this possible to do with XPath?
>
> Thanks,
> Daniel.
>
Figured it out: ./ValueOverride|./ValueDefault[../ValueOverride='']

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Roedy Green on
On Fri, 12 Mar 2010 15:38:05 -0800, Daniel Pitts
<newsgroup.spamfilter(a)virtualinfinity.net> wrote, quoted or indirectly
quoted someone who said :

>> I need a single XPath expression that will return ValueOverride if it is
>> non-empty, and ValueDefault otherwise.

That kind of problem is solved with XOR in Java, perhaps in XPATH too.
--
Roedy Green Canadian Mind Products
http://mindprod.com

The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.
~ Tom Cargill
From: Arne Vajhøj on
On 12-03-2010 22:23, Roedy Green wrote:
> On Fri, 12 Mar 2010 15:38:05 -0800, Daniel Pitts
> <newsgroup.spamfilter(a)virtualinfinity.net> wrote, quoted or indirectly
> quoted someone who said :
>
>>> I need a single XPath expression that will return ValueOverride if it is
>>> non-empty, and ValueDefault otherwise.
>
> That kind of problem is solved with XOR in Java,

It is not.

See:
http://en.wikipedia.org/wiki/Xor

> perhaps in XPATH too.

Neither.

Arne

From: Mike Schilling on
Daniel Pitts wrote:
> On 3/12/2010 2:56 PM, Daniel Pitts wrote:
>> I have a document which has two elements: ValueOverride and
>> ValueDefault For example:
>> <MyDoc>
>> <ValueOverride>The override value</ValueOverride>
>> <ValueDefault>The default value</ValueDefault>
>> </MyDoc>
>>
>> I need a single XPath expression that will return ValueOverride if
>> it is non-empty, and ValueDefault otherwise.
>>
>> Is this possible to do with XPath?
>>
>> Thanks,
>> Daniel.
>>
> Figured it out: ./ValueOverride|./ValueDefault[../ValueOverride='']

What do you mean by "non-empty'? If you mean "has no text nodes for
children", that expression doesn't work for the document

<MyDoc>
<ValueOverride/>
<ValueDefault>The default value</ValueDefault>
</MyDoc>

It returns two nodes, the elements ValueOverride.and ValueDefault.

I'm guessing that what you want is the string whose parent is ValueOverride,
if there is one, and if not, the string whose parent is ValueDefault. In
that case, try

string(./ValueOverride[count(./text()) > 0]|./ValueDefault)

This uses the fact that string(), like the other conversion functions, when
applied to a node-set ignores any nodes after the first.