From: Daniel Pitts on 13 Mar 2010 20:16 On 3/12/2010 8:31 PM, Mike Schilling wrote: > 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. Strange, the solution I posted worked just fine for me. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Mike Schilling on 13 Mar 2010 21:13 Daniel Pitts wrote: > On 3/12/2010 8:31 PM, Mike Schilling wrote: >> 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. > Strange, the solution I posted worked just fine for me. Then you must not have applied it to <MyDoc> <ValueOverride/> <ValueDefault>The default value</ValueDefault> </MyDoc> And perhaps that's not a document you need to apply it to. It was difficult to tell from the description of the problem.
From: Daniel Pitts on 15 Mar 2010 13:27 On 3/13/2010 6:13 PM, Mike Schilling wrote: > Daniel Pitts wrote: >> On 3/12/2010 8:31 PM, Mike Schilling wrote: >>> 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. >> Strange, the solution I posted worked just fine for me. > > Then you must not have applied it to > > <MyDoc> > <ValueOverride/> > <ValueDefault>The default value</ValueDefault> > </MyDoc> > > And perhaps that's not a document you need to apply it to. It was difficult > to tell from the description of the problem. Are you saying that <ValueOverride></ValueOverride> will have a different value than <ValueOverride/>? In theory, I won't have a case where ValueOverride uses the shortcut. I should verify that my code works with either, but I'd be surprised if it doesn't. When I have some time, I'll write up an SSCCE to see. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Mike Schilling on 15 Mar 2010 14:25 Daniel Pitts wrote: > On 3/13/2010 6:13 PM, Mike Schilling wrote: >> Daniel Pitts wrote: >>> On 3/12/2010 8:31 PM, Mike Schilling wrote: >>>> 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. >>> Strange, the solution I posted worked just fine for me. >> >> Then you must not have applied it to >> >> <MyDoc> >> <ValueOverride/> >> <ValueDefault>The default value</ValueDefault> >> </MyDoc> >> >> And perhaps that's not a document you need to apply it to. It was >> difficult to tell from the description of the problem. > Are you saying that <ValueOverride></ValueOverride> will have a > different value than <ValueOverride/>? No, they're identical. But if you have either one of them, it'll match your XPath expression, even though it doesn't specify a value (or, to be more precise, doesn't have any child nodes that are text.)
From: Daniel Pitts on 15 Mar 2010 15:17 On 3/15/2010 11:25 AM, Mike Schilling wrote: > Daniel Pitts wrote: >> On 3/13/2010 6:13 PM, Mike Schilling wrote: >>> Daniel Pitts wrote: >>>> On 3/12/2010 8:31 PM, Mike Schilling wrote: >>>>> 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. >>>> Strange, the solution I posted worked just fine for me. >>> >>> Then you must not have applied it to >>> >>> <MyDoc> >>> <ValueOverride/> >>> <ValueDefault>The default value</ValueDefault> >>> </MyDoc> >>> >>> And perhaps that's not a document you need to apply it to. It was >>> difficult to tell from the description of the problem. >> Are you saying that<ValueOverride></ValueOverride> will have a >> different value than<ValueOverride/>? > > No, they're identical. But if you have either one of them, it'll match your > XPath expression, even though it doesn't specify a value (or, to be more > precise, doesn't have any child nodes that are text.) > > So the result will be "The default value", which is what I want. Or are you saying it is document order-dependent? If the latter is the case, then my code still works for my situation, but should be fixed in either case ;-) -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Date comparisons Next: Problem in executing multithreaded program |