Aleski
Aleski

Reputation: 1442

How do you use XPath to select values based between a range of a specific attribute

I have the following xml node that is repeated per country. Here is an example one:

<country name="Afghanistan" population="22664136" area="647500">

I want to apply a template on a specific set range of data based on the value of the attribute "population". Specifically I want to pull back all that are larger than 9 million (9000000) and smaller than 10 million (10000000).

Im not terribly comfortable yet with XSLT and XPath. Here is what I expected to work:

<xsl:apply-templates select="country[@population > '9000000' and population < '10000000']"/>

However this throws an error based on the '<' character in the value range.

I did a bit of google-fu and I couldnt find anything to shed some light on what I should be doing.

Thanks in advance.

Upvotes: 1

Views: 1293

Answers (2)

Sandeep Sukhija
Sandeep Sukhija

Reputation: 1176

Since XSLT is a XML syntax, If you use > and <, the interpreter will understand the start or end of tag.

use &lt; and &gt; instead of < and >,

so your xpath would become <xsl:apply-templates select="country[@population &gt; '9000000' and @population &lt; '10000000']"/>

Upvotes: 2

JLRishe
JLRishe

Reputation: 101730

You have to escape the < character because XSLT is XML, and use a @ before population both times.

And there's no need for quotes around the numbers:

<xsl:apply-templates select="country[@population > 9000000 and 
                                     @population &lt; 10000000]" />

Upvotes: 2

Related Questions