[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] General rule for designing XPath expressions to return items in document order?


Subject: [xsl] General rule for designing XPath expressions to return items in document order?
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Wed, 8 Jan 2014 10:33:44 +0000

Hi Folks,

This XPath expression says: select all <section> element's <head> element:

	//section/head

But that may not result in returning nodes in document order, as is the case
for this input:

<section>
	<section>
		<head/>	<!-- A -->
	</section>
	<head/>	    	<!-- B -->
</section>

"A" occurs first in the document, but is returned second. "B" occurs second in
the document, but is returned first.

To ensure that the <head> elements are selected in document order we can
rewrite the XPath expression to select all <head> elements that have a parent
<section> element:

	/descendant::head[parent::section]

Now the first <head> element in the document is the first item returned. The
second <head> element in the document is the second item returned.

I am seeking a general rule for designing XPath expressions to return the
selected items in document order. Can you provide a general rule?

/Roger


Current Thread
Keywords