# If else on xpath value in Logstash

**URL:** <https://discuss.elastic.co/t/if-else-on-xpath-value-in-logstash/119709>\
**Category:** Logstash\
**Created:** [February 13, 2018, 9:33pm UTC](https://discuss.elastic.co/t/if-else-on-xpath-value-in-logstash/119709 "2018-02-13T21:33:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karanushah.1992](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karanushah.1992](https://discuss.elastic.co/u/Karanushah.1992)\
**Post date:** [February 13, 2018, 9:33pm UTC](https://discuss.elastic.co/t/if-else-on-xpath-value-in-logstash/119709/1 "2018-02-13T21:33:46Z")

</div>

Hi,

I am trying to create two new fields based on the value of xpath. For example if the station\_name is "Finch" then create a field name "ny\_station" else store the value in "nonny\_stations". Can anyone help me that below is the codified version of what I am trying to achieve.

if "Finch" in [station\_name] {  
xpath =\> ["/station/name/text()","Ny\_station"]  
}  
else {  
xpath =\> ["/station/name/text()","nonNy\_station"]  
}

Here is the structure of my xml file

 ![xml%20structure](https://us1.discourse-cdn.com/elastic/original/3X/7/a/7a409630cb73aedc536f35c9d9d45afe2ccd8f84.png)

Here is my config file  
input  
{  
file  
{  
path =\> "C:\Users\186181152\Downloads\stations3.xml"  
start\_position =\> "beginning"  
sincedb\_path =\> "/dev/null"  
exclude =\> "\*.gz"  
type =\> "xml"  
codec =\> multiline {  
pattern =\> ""  
negate =\> "true"  
what =\> "previous"  
}  
}  
}  
filter  
{  
xml  
{  
source =\> "message"  
store\_xml =\> false  
target =\> "stations"  
xpath =\> [  
"/stations/station/id/text()", "station\_id",  
"/stations/station/name/text()", "station\_name"  
]  
}

}

output  
{  
elasticsearch  
{  
codec =\> json  
hosts =\> "localhost"  
index =\> "xmlns24"  
}  
stdout  
{  
codec =\> rubydebug  
}  
}

---

<div class="post-metadata">

**Author:** ![Karanushah.1992](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karanushah.1992](https://discuss.elastic.co/u/Karanushah.1992)\
**Post date:** [February 14, 2018, 3:03pm UTC](https://discuss.elastic.co/t/if-else-on-xpath-value-in-logstash/119709/2 "2018-02-14T15:03:00Z")

</div>

HI, This is the working solution to what I want to achieve

Following is the  
if "Finch" in [station\_name]  
{ mutate  
{ add\_field =\> {"Ny\_station" =\> "%{station\_name}"}  
}  
}  
else  
{ mutate  
{ add\_field =\> {"nonNy\_station" =\> "%{station\_name}"}  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 14, 2018, 3:03pm UTC](https://discuss.elastic.co/t/if-else-on-xpath-value-in-logstash/119709/3 "2018-03-14T15:03:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
