Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xslt select between 2 values

Tags:

xml

xslt

xpath

Is it possible to select only those values that lay between 2 given values using xslt?

i.e.

<value>1</value> 
<value>1.2</value>
<value>1.3</value>
<value>1.4</value>
<value>1.5</value>
<value>2</value> 
<value>2.1</value>
<value>2.3</value>
<value>2.4</value>
<value>2.5</value>

I only want to display values between 1 and 2.

like image 833
user1949157 Avatar asked Jul 21 '26 16:07

user1949157


2 Answers

Unlike the other answer I prefer to go with Identity Override

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="value[. &lt; 1 or . &gt; 2]"/>
</xsl:stylesheet>

outputs:

<root>
  <value>1</value> 
  <value>1.2</value>
  <value>1.3</value>
  <value>1.4</value>
  <value>1.5</value>
  <value>2</value> 
</root>
like image 120
InfantPro'Aravind' Avatar answered Jul 23 '26 16:07

InfantPro'Aravind'


Certainly:

select="value[. &gt; 1 and . &lt; 2]"

if you want to include 1 and 2:

select="value[. &gt;= 1 and . &lt;= 2]"

if the top and bottom limits are in variables (using $min and $max as examples here):

select="value[. &gt; $min and . &lt; $max]"
like image 25
JLRishe Avatar answered Jul 23 '26 15:07

JLRishe