# XML attributes and arrays

**URL:** <https://discuss.elastic.co/t/xml-attributes-and-arrays/66522>\
**Category:** Logstash\
**Created:** [November 18, 2016, 10:59am UTC](https://discuss.elastic.co/t/xml-attributes-and-arrays/66522 "2016-11-18T10:59:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![espogian](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@espogian](https://discuss.elastic.co/u/espogian)\
**Post date:** [November 18, 2016, 10:59am UTC](https://discuss.elastic.co/t/xml-attributes-and-arrays/66522/1 "2016-11-18T10:59:38Z")

</div>

Hi, what about parsing this XML with Logstash

```
<root>
  <level1>
    <item name="item1" value="10">
    <item name="item2" value="20">
    <item name="item3" value="30">
    </service>
  </level1>
</root>

```

I'm using the following Xpath filter:

```
xml {
  source => "message"
  store_xml => "false"
  remove_namespaces => "true"
  xpath => ["root/level1/item/@name", "ItemName"]
  xpath => ["root/level1/item/@value", "ItemValue"]
}

```

However, this creates two separate arrays: `[item1, item2, item3]` and `[10, 20, 30]`.  
I would like to keep the correspondence between items and values.

Any ideas?

---

<div class="post-metadata">

**Author:** ![espogian](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@espogian](https://discuss.elastic.co/u/espogian)\
**Post date:** [November 18, 2016, 4:42pm UTC](https://discuss.elastic.co/t/xml-attributes-and-arrays/66522/2 "2016-11-18T16:42:00Z")

</div>

I think I've found a viable solution, but it requires perfect knowledge of all possible attributes names:

```
xml {
  source => "message"
  store_xml => "false"
  remove_namespaces => "true"
  xpath => ["root/level1/item[@name='item1']/@value", "Item1Value"]
  xpath => ["root/level1/item[@name='item2']/@value", "Item2Value"]
  xpath => ["root/level1/item[@name='item3']/@value", "Item3Value"]
}
```

---

<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:** [November 22, 2016, 9:06pm UTC](https://discuss.elastic.co/t/xml-attributes-and-arrays/66522/3 "2016-11-22T21:06:52Z")

</div>

Instead of using XPath, can you parse and store the whole document? And weed out the fields you don't need (the prune filter might help)? Alternatively, use a ruby filter to join the two arrays in your first solution.

---

<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:** [December 20, 2016, 9:06pm UTC](https://discuss.elastic.co/t/xml-attributes-and-arrays/66522/4 "2016-12-20T21:06:59Z")

</div>

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