# Problem with xml filter

**URL:** https://discuss.elastic.co/t/problem-with-xml-filter/80725
**Category:** Logstash
**Created:** [March 30, 2017, 5:52pm UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725 "2017-03-30T17:52:42Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![flalar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flalar/32/85777_2.png) [@flalar](https://discuss.elastic.co/u/flalar)
#### Post date: [March 30, 2017, 5:52pm UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/1 "2017-03-30T17:52:42Z")

</div>

We're having issues setting a date pulled from a xml document as @timestamp in the date filter.

input xml looks like this  
`<Task> <TaskId>ServerTasks-5017</TaskId> <TaskState>Success</TaskState> <Created>2015-12-22T08:20:03</Created> <QueueTime>2015-12-22T08:20:03</QueueTime> <StartTime>2015-12-22T08:20:06</StartTime> <CompletedTime>2015-12-22T08:21:11</CompletedTime> <DurationSeconds>68</DurationSeconds> </Task>`

Filer looks like this

`filter { xml { source => "message" target => "xml_content" xpath => ["//Created/text()", "created"] } date { match => ["created","yyyy-MM-dd'T'HH:mm:ss"] #match => ["xml_content.Created", "yyyy-MM-dd'T'HH:mm:ss"] } }`

As you can see I've both tried to match the content pulled from the xml directly and with the xpath.

Both end up with \_dateparsefailure and the @timestamp is set the the logstash time.

Any tips for a novice logstash user on how to resolve this?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 30, 2017, 7:21pm UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/2 "2017-03-30T19:21:00Z")

</div>

I'm not XPath-fluent, but shouldn't the expression be //Task/Created/text()? What does the resulting event look like? Use a `stdout { codec => rubydebug }` output. Also, what's in the logs? When the date filter fails it tells you why in the log.

---

<div class="post-metadata">

### Author: ![flalar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flalar/32/85777_2.png) [@flalar](https://discuss.elastic.co/u/flalar)
#### Post date: [March 30, 2017, 8:17pm UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/3 "2017-03-30T20:17:46Z")

</div>

timestamp and "created" looks like this

"@timestamp" =\> 2017-03-30T18:29:09.354Z,  
"created" =\> [  
[0] "2015-12-22T08:20:03"  
],  
"@version" =\> "1",

Might be looking in the wrong log but cant find anything relating to \_dateparsefailure in the log. Even tried to set level to debug.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 30, 2017, 8:21pm UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/4 "2017-03-30T20:21:48Z")

</div>

The problem is that `created` is an array. Disable the xml filter's `force_array` option.

---

<div class="post-metadata">

### Author: ![flalar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flalar/32/85777_2.png) [@flalar](https://discuss.elastic.co/u/flalar)
#### Post date: [March 30, 2017, 9:45pm UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/5 "2017-03-30T21:45:53Z")

</div>

Thanks for your help! Much appreciated!

even setting 'force\_array' to false gives xpath extracted element 'created' as array. Rest of the content that is not extracted with xpath is now not in array format.

Any tips? Can i reference the first element of the array 'created' somehow?

---

<div class="post-metadata">

### Author: ![jordansissel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jordansissel/32/44957_2.png) [@jordansissel](https://discuss.elastic.co/u/jordansissel)
#### Post date: [March 30, 2017, 9:57pm UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/6 "2017-03-30T21:57:40Z")

</div>

I can reproduce your behavior:

```auto
% bin/logstash -e 'filter { xml { source => "message" xpath => { "//Created/text()" => "foo" } store_xml => false } } '

{
  "@timestamp" => 2017-03-29T06:49:47.122Z,
  "foo" => [
    [0] "2015-12-22T08:20:03"
  ],
...

```

I agree this behavior is not what I'd expect 😉

There's an open issue [on the logstash-filter-xml project](https://github.com/logstash-plugins/logstash-filter-xml/issues/36) that mentions this issue. Your feedback on this issue is welcome 🙂

---

<div class="post-metadata">

### Author: ![GrahamHannington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grahamhannington/32/4404_2.png) [@GrahamHannington](https://discuss.elastic.co/u/GrahamHannington)
#### Post date: [March 31, 2017, 1:47am UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/7 "2017-03-31T01:47:10Z")

</div>

> [@flalar](#):
>
> Can i reference the first element of the array 'created' somehow?

Yes.

Until the issue mentioned by @jordansissel is resolved, insert the following `mutate` filter after the `xml` filter:

```
mutate {
  replace => { "created" => "%{created[0]}" }
}

```

This replaces the array with the value of the first (and, in this case, only) element.

That is, instead of:

```
"created":["2015-12-22T08:20:03"]

```

the output contains:

```
"created":"2015-12-22T08:20:03"

```

---

<div class="post-metadata">

### Author: ![GrahamHannington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grahamhannington/32/4404_2.png) [@GrahamHannington](https://discuss.elastic.co/u/GrahamHannington)
#### Post date: [March 31, 2017, 2:52am UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/8 "2017-03-31T02:52:15Z")

</div>

Regarding the following `match` setting (commented-out in your example):

```
match => ["xml_content.Created", "yyyy-MM-dd'T'HH:mm:ss"]

```

From the Elastic docs topic “[Accessing Event Data and Fields in the Configuration](https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html)”:

> The syntax to access a field is [fieldname]. If you are referring to a top-level field, you can omit the and simply use fieldname. To refer to a nested field, you specify the full path to that field: [top-level field][nested field].

Replace `xml_content.Created` with `xml_content[Created]`

Works with your sample XML data in Logstash 5.2.1, with—as recommended by @magnusbaeck—`force_array => false`.

---

<div class="post-metadata">

### Author: ![GrahamHannington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grahamhannington/32/4404_2.png) [@GrahamHannington](https://discuss.elastic.co/u/GrahamHannington)
#### Post date: [March 31, 2017, 3:02am UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/9 "2017-03-31T03:02:51Z")

</div>

> [@magnusbaeck](#):
>
> shouldn't the expression be //Task/Created/text()

The expression that you cite and the original expression will both work. However, `/Task/Created/text()` (note the single leading slash) is better.

From the [XPath W3C recommendation](https://www.w3.org/TR/xpath/):

> // is short for /descendant-or-self::node()/.

A more specific path, such as `/Task/Created`, is typically more efficient (and less likely to select nodes that you didn’t intend to 🙂).

---

<div class="post-metadata">

### Author: ![GrahamHannington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grahamhannington/32/4404_2.png) [@GrahamHannington](https://discuss.elastic.co/u/GrahamHannington)
#### Post date: [March 31, 2017, 6:03am UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/10 "2017-03-31T06:03:57Z")

</div>

Try this filter:

```
filter {
    xml {
        source => "message"
        target => "@metadata[xml_content]"
        force_array => false
    }
    # Copy XML content to first-level fields with all-lowercase names
    ruby {
        code => '
            event.get("@metadata[xml_content]").each do |key, value|
                event.set(key.downcase, value)
            end
        '
    }
    mutate {
         remove_field => ["message", "@metadata"]
         convert => {
           "durationseconds" => "integer"
         }
    }
    date {
        match => ["created", "ISO8601"]
    }
}

```

**Notes:**

- @magnusbaeck: I thought `@metadata` wasn’t supposed to get passed through to the output, but it does get included in output to stdin and Elasticsearch. Hence its presence in `remove_field`. Did I miss a memo? (I’m using Logstash 5.2.1.)
- That Ruby code is a workaround for the issue I describe in “[Set target of xml filter to root?](https://discuss.elastic.co/t/set-target-of-xml-filter-to-root/80770)”
- If you want to preserve the original case of the XML element names, remove `.downcase` from `key.downcase`

### Example Logstash output

In JSON format:

```
"@timestamp": "2015-12-22T08:20:03.000Z",
"completedtime": "2015-12-22T08:21:11",
"created": "2015-12-22T08:20:03",
"@version": "1",
"host": "58a3fe88f636",
"starttime": "2015-12-22T08:20:06",
"durationseconds": 68,
"taskid": "ServerTasks-5017",
"queuetime": "2015-12-22T08:20:03",
"taskstate": "Success"

```

### More unsolicited tips

If you can—if you are responsible for creating the original XML-format events—consider adding a zone designator to the time stamps. Otherwise, be sure that you understand the repercussions of specifying local times, and how those values might be interpreted.

---

<div class="post-metadata">

### Author: ![GrahamHannington](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/grahamhannington/32/4404_2.png) [@GrahamHannington](https://discuss.elastic.co/u/GrahamHannington)
#### Post date: [March 31, 2017, 8:27am UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/11 "2017-03-31T08:27:42Z")

</div>

> [@jordansissel](#):
>
> Your feedback on this issue is welcome 🙂

I have added [my two cents](https://github.com/logstash-plugins/logstash-filter-xml/issues/36#issuecomment-290648647) to that issue.

---

<div class="post-metadata">

### Author: ![flalar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flalar/32/85777_2.png) [@flalar](https://discuss.elastic.co/u/flalar)
#### Post date: [April 4, 2017, 7:37am UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/12 "2017-04-04T07:37:35Z")

</div>

This works perfectly! Thank you all for all tips!

---

<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: [May 2, 2017, 7:37am UTC](https://discuss.elastic.co/t/problem-with-xml-filter/80725/13 "2017-05-02T07:37:49Z")

</div>

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