You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Nikolaos Giannadakis <ng...@doc.ic.ac.uk> on 2001/07/04 14:34:16 UTC

XPath riddle

Dear All,

Can you tell what the XPath expression that:
"selects all C elements that come after A and have a D parent" is.

That is, there might be a schema, which declares the unwanted instances of C
as integers, while
the other C declared has some anonymous complexType.


<?xml version="1.0" encoding="UTF-8"?>
<A>
	<B>
		<C/>						<!-- DO NOT select this -->
		<D>
			<!-- recursion is introduced here -->
			<C>					<!-- select this -->
				<B>
					<C/>			<!-- DO NOT select this -->
					<D>
						<C/>		<!-- select this -->
					</D>
				</B>
			</C>
		</D>
	</B>
</A>


/A//D/C (/A/descendant::D/C) would suffice, or, better, /A//B/D/C
(/A/descendant::B/D/C). But this would not rule out the possibility of the
B/D/C pattern appearing somewhere after A in another context. I cannot find
any XPath feature that would handle recursion.


<?xml version="1.0" encoding="UTF-8"?>
<A>
	<B>
		<F>
			<B>
				<D>
					<C/>			<!-- this would be selected incorrectly -->
				</D>
			</B>
		</F>
		<D>
			<!-- recursion is introduced here -->
			<C>					<!-- select this -->
				<B>
					<C/>			<!-- DO NOT select this -->
					<D>
						<C/>		<!-- select this -->
					</D>
				</B>
			</C>
		</D>
	</B>
</A>

Using /A/B/D/C | /A/B/D/C//B/D/C would overcome this, but you can see how I
could create another problematic example...
How does one find one's way around this, using a generic XPath approach?
I am not saying this is good XML design. To the contrary! ... it is legal,
nonetheless ...any ideas?

regards,

nikolas/


Re: XPath riddle

Posted by Gary L Peskin <ga...@firstech.com>.
Will something like this help?

  /A//D/C[text()]

Gary


Nicholas Giannadakis wrote:
> >
> >Second, I'm pretty confused about what you're trying to do.  I
> >understand the first example, but why should the first <C/> element
> >-not- be selected in the second example?  It is a "C element that comes
> >after A and has a D parent".
> 
> let's say that the Cs that must not be selected have different types.
> Since the scopes of the corresponding schema definitions would be
> different, one would possibly like to have the Cs that come with
> xsd:string type and not those with xsd:integer for instance.

Re: XPath riddle

Posted by Nicholas Giannadakis <ng...@doc.ic.ac.uk>.
On Wed, 4 Jul 2001, Gary L Peskin wrote:

>Nikolaos --
>
>First, since this is not a Xalan-specific question, you'd probably get a
>faster response from the Mulberry list.

sorry!

>
>Second, I'm pretty confused about what you're trying to do.  I
>understand the first example, but why should the first <C/> element
>-not- be selected in the second example?  It is a "C element that comes
>after A and has a D parent".

let's say that the Cs that must not be selected have different types.
Since the scopes of the corresponding schema definitions would be
different, one would possibly like to have the Cs that come with
xsd:string type and not those with xsd:integer for instance.



>
>Gary
>
>Nikolaos Giannadakis wrote:
>>
>> Dear All,
>>
>> Can you tell what the XPath expression that:
>> "selects all C elements that come after A and have a D parent" is.
>>
>> That is, there might be a schema, which declares the unwanted instances of C
>> as integers, while
>> the other C declared has some anonymous complexType.
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <A>
>>         <B>
>>                 <C/>                                            <!-- DO NOT select this -->
>>                 <D>
>>                         <!-- recursion is introduced here -->
>>                         <C>                                     <!-- select this -->
>>                                 <B>
>>                                         <C/>                    <!-- DO NOT select this -->
>>                                         <D>
>>                                                 <C/>            <!-- select this -->
>>                                         </D>
>>                                 </B>
>>                         </C>
>>                 </D>
>>         </B>
>> </A>
>>
>> /A//D/C (/A/descendant::D/C) would suffice, or, better, /A//B/D/C
>> (/A/descendant::B/D/C). But this would not rule out the possibility of the
>> B/D/C pattern appearing somewhere after A in another context. I cannot find
>> any XPath feature that would handle recursion.
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <A>
>>         <B>
>>                 <F>
>>                         <B>
>>                                 <D>
>>                                         <C/>                    <!-- this would be selected incorrectly -->
>>                                 </D>
>>                         </B>
>>                 </F>
>>                 <D>
>>                         <!-- recursion is introduced here -->
>>                         <C>                                     <!-- select this -->
>>                                 <B>
>>                                         <C/>                    <!-- DO NOT select this -->
>>                                         <D>
>>                                                 <C/>            <!-- select this -->
>>                                         </D>
>>                                 </B>
>>                         </C>
>>                 </D>
>>         </B>
>> </A>
>>
>> Using /A/B/D/C | /A/B/D/C//B/D/C would overcome this, but you can see how I
>> could create another problematic example...
>> How does one find one's way around this, using a generic XPath approach?
>> I am not saying this is good XML design. To the contrary! ... it is legal,
>> nonetheless ...any ideas?
>>
>> regards,
>>
>> nikolas/
>


Re: XPath riddle

Posted by Gary L Peskin <ga...@firstech.com>.
Nikolaos --

First, since this is not a Xalan-specific question, you'd probably get a
faster response from the Mulberry list.

Second, I'm pretty confused about what you're trying to do.  I
understand the first example, but why should the first <C/> element
-not- be selected in the second example?  It is a "C element that comes
after A and has a D parent".

Gary

Nikolaos Giannadakis wrote:
> 
> Dear All,
> 
> Can you tell what the XPath expression that:
> "selects all C elements that come after A and have a D parent" is.
> 
> That is, there might be a schema, which declares the unwanted instances of C
> as integers, while
> the other C declared has some anonymous complexType.
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <A>
>         <B>
>                 <C/>                                            <!-- DO NOT select this -->
>                 <D>
>                         <!-- recursion is introduced here -->
>                         <C>                                     <!-- select this -->
>                                 <B>
>                                         <C/>                    <!-- DO NOT select this -->
>                                         <D>
>                                                 <C/>            <!-- select this -->
>                                         </D>
>                                 </B>
>                         </C>
>                 </D>
>         </B>
> </A>
> 
> /A//D/C (/A/descendant::D/C) would suffice, or, better, /A//B/D/C
> (/A/descendant::B/D/C). But this would not rule out the possibility of the
> B/D/C pattern appearing somewhere after A in another context. I cannot find
> any XPath feature that would handle recursion.
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <A>
>         <B>
>                 <F>
>                         <B>
>                                 <D>
>                                         <C/>                    <!-- this would be selected incorrectly -->
>                                 </D>
>                         </B>
>                 </F>
>                 <D>
>                         <!-- recursion is introduced here -->
>                         <C>                                     <!-- select this -->
>                                 <B>
>                                         <C/>                    <!-- DO NOT select this -->
>                                         <D>
>                                                 <C/>            <!-- select this -->
>                                         </D>
>                                 </B>
>                         </C>
>                 </D>
>         </B>
> </A>
> 
> Using /A/B/D/C | /A/B/D/C//B/D/C would overcome this, but you can see how I
> could create another problematic example...
> How does one find one's way around this, using a generic XPath approach?
> I am not saying this is good XML design. To the contrary! ... it is legal,
> nonetheless ...any ideas?
> 
> regards,
> 
> nikolas/

Re: XPath riddle

Posted by Tom Bradford <br...@dbxmlgroup.com>.
Nikolaos Giannadakis wrote:
> Dear All,
> 
> Can you tell what the XPath expression that:
> "selects all C elements that come after A and have a D parent" is.
> 
> That is, there might be a schema, which declares the unwanted instances of C
> as integers, while
> the other C declared has some anonymous complexType.

This brings up an interesting limitation of XPaths as they were designed
predicated on the nature of DTDs, where when you defined an element,
that was the definition for that element in any context, and so
//element XPaths would work in just about all cases.  XML Schemas throw
that design pattern for a loop, in that, not only does the element
matter, but its entire ancestor list plays into the context of the
element.

Time to fix that one, too.

-- 
Tom Bradford --- The dbXML Project --- http://www.dbxml.org/
We store your XML data a hell of a lot better than /dev/null