<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: parsing XML data with hierarchy of KV pairs in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259305#M77740</link>
    <description>&lt;P&gt;that is so friggin cool. thanks for showing that!&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jul 2016 17:21:54 GMT</pubDate>
    <dc:creator>sloshburch</dc:creator>
    <dc:date>2016-07-14T17:21:54Z</dc:date>
    <item>
      <title>parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259294#M77729</link>
      <description>&lt;P&gt;Banging my head on this one for too long, could use some help.&lt;/P&gt;

&lt;P&gt;Take a sample doc such as the below, where you have a hierarchy of KV pairs:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;root&amp;gt;
  &amp;lt;ItemSets&amp;gt;
    &amp;lt;ItemCollection&amp;gt;
      &amp;lt;Name&amp;gt;name1&amp;lt;/Name&amp;gt;
      &amp;lt;Value&amp;gt;value1&amp;lt;/Value&amp;gt;
    &amp;lt;/ItemCollection&amp;gt;
    &amp;lt;ItemCollection&amp;gt;
      &amp;lt;Name&amp;gt;name2&amp;lt;/Name&amp;gt;
      &amp;lt;Value&amp;gt;value2&amp;lt;/Value&amp;gt;
    &amp;lt;/ItemCollection&amp;gt;
    &amp;lt;ItemCollection&amp;gt;
      &amp;lt;Name&amp;gt;name3&amp;lt;/Name&amp;gt;
      &amp;lt;Value&amp;gt;value3&amp;lt;/Value&amp;gt;
    &amp;lt;/ItemCollection&amp;gt;
&amp;lt;/ItemSets&amp;gt;
&amp;lt;/root&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;We want to do stuff based the values of keys. But due to the field naming, a “naïve” approach using spath’d dot notation won’t work, because you have n “root.ItemSet.ItemCollection.Name” fields in the same event. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | spath | search root.ItemSets.ItemCollection.Name="name1"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And I cannot rely on the ordering, so using an array index won't help. I started down the route of xpath:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | xpath "//ItemCollection/Name" outfield=xml | search xml="name1"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But that hasn't gotten me any closer, because the goal isn't to find the one element, it's to associate that element value (say "name1") to its sibling ("value1") in a search, and then either return the value, or he whole event.&lt;/P&gt;

&lt;P&gt;In pseudocode, I want this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;for each ItemCollection {
  if Name = "name1" {
    print Value # or print Event
  }
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Been playing around with makemv, but don't have anything worth showing off and I'm not sold on that technique anyway.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 16:02:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259294#M77729</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-07-13T16:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259295#M77730</link>
      <description>&lt;P&gt;What you can do is to add a expression (square bracket notation in XPath) at the specific position of the XPath expression, which will give you that "where" type of filtering.&lt;/P&gt;

&lt;P&gt;If you try the following example where you're looking for Name = 'name2' ...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|makeresults
| eval _raw="&amp;lt;root&amp;gt;&amp;lt;ItemSets&amp;gt;&amp;lt;ItemCollection&amp;gt;&amp;lt;Name&amp;gt;name1&amp;lt;/Name&amp;gt;&amp;lt;Value&amp;gt;value1&amp;lt;/Value&amp;gt;&amp;lt;/ItemCollection&amp;gt;&amp;lt;ItemCollection&amp;gt;&amp;lt;Name&amp;gt;name2&amp;lt;/Name&amp;gt;&amp;lt;Value&amp;gt;value2&amp;lt;/Value&amp;gt;&amp;lt;/ItemCollection&amp;gt;&amp;lt;ItemCollection&amp;gt;&amp;lt;Name&amp;gt;name3&amp;lt;/Name&amp;gt;&amp;lt;Value&amp;gt;value3&amp;lt;/Value&amp;gt;&amp;lt;/ItemCollection&amp;gt;&amp;lt;/ItemSets&amp;gt;&amp;lt;/root&amp;gt;"
| xpath "//root/ItemSets/ItemCollection[Name/text()='name2']/Value" outfield=value
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You should get the result "value2" in the value field.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 16:21:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259295#M77730</guid>
      <dc:creator>ekim_splunk</dc:creator>
      <dc:date>2016-07-13T16:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259296#M77731</link>
      <description>&lt;P&gt;@halr9000 - is the doc above equal to one event ?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 16:40:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259296#M77731</guid>
      <dc:creator>aljohnson_splun</dc:creator>
      <dc:date>2016-07-13T16:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259297#M77732</link>
      <description>&lt;P&gt;Like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | rex max_match=0 field=_raw "(?msi)&amp;lt;Name&amp;gt;(?&amp;lt;KEY&amp;gt;.*?)&amp;lt;\/Name&amp;gt;\s+&amp;lt;Value&amp;gt;(?&amp;lt;VAL&amp;gt;.*?)&amp;lt;\/Value&amp;gt;"
| eval _raw=mvzip(KEY,VAL, "=") | fields - KEY VAL
| extract limit=0 mv_add=t kvdelim="="
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This give you:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;name1     name2     name3
value1    value2    value3
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jul 2016 16:48:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259297#M77732</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2016-07-13T16:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259298#M77733</link>
      <description>&lt;P&gt;Would this maybe work for you?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | fields - count
| eval _raw = "
 &amp;lt;root&amp;gt;
   &amp;lt;ItemSets&amp;gt;
     &amp;lt;ItemCollection&amp;gt;
       &amp;lt;Name&amp;gt;name1&amp;lt;/Name&amp;gt;
       &amp;lt;Value&amp;gt;value1&amp;lt;/Value&amp;gt;
     &amp;lt;/ItemCollection&amp;gt;
     &amp;lt;ItemCollection&amp;gt;
       &amp;lt;Name&amp;gt;name2&amp;lt;/Name&amp;gt;
       &amp;lt;Value&amp;gt;value2&amp;lt;/Value&amp;gt;
     &amp;lt;/ItemCollection&amp;gt;
     &amp;lt;ItemCollection&amp;gt;
       &amp;lt;Name&amp;gt;name3&amp;lt;/Name&amp;gt;
       &amp;lt;Value&amp;gt;value3&amp;lt;/Value&amp;gt;
     &amp;lt;/ItemCollection&amp;gt;
 &amp;lt;/ItemSets&amp;gt;
 &amp;lt;/root&amp;gt;
"
| spath path=root.ItemSets.ItemCollection output=ItemCollections
| mvexpand ItemCollections
| spath input=ItemCollections
| search Name=name1
| table Value
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Value
-----------
value1 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Jul 2016 16:49:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259298#M77733</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-07-13T16:49:18Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259299#M77734</link>
      <description>&lt;P&gt;Yup one event&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 18:16:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259299#M77734</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-07-13T18:16:26Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259300#M77735</link>
      <description>&lt;P&gt;Thanks! Accepting this one because:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;XPath makes my head hurt&lt;/LI&gt;
&lt;LI&gt;This solution feels really Splunky and I really needed a good mvexpand example. &lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Wed, 13 Jul 2016 20:33:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259300#M77735</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-07-13T20:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259301#M77736</link>
      <description>&lt;P&gt;After playing for another 30 min with the actual data, I could not get this technique to work. I've decided that it's me, and that I don't like XPath. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 20:35:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259301#M77736</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-07-13T20:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259302#M77737</link>
      <description>&lt;P&gt;This reminds me of &lt;A href="http://stackoverflow.com/a/1732454/6637"&gt;the old story about parsing HTML with regular expressions&lt;/A&gt;. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 20:36:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259302#M77737</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-07-13T20:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259303#M77738</link>
      <description>&lt;P&gt;If this were 10 years ago, when XML was all the rage, I'd wax eloquently about why you should love XPath. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; If ever you did want to get deep down and dirty with XPath (which sounds like you probably won't), I'd always be happy to help. &lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 21:43:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259303#M77738</guid>
      <dc:creator>ekim_splunk</dc:creator>
      <dc:date>2016-07-13T21:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259304#M77739</link>
      <description>&lt;P&gt;thx! I knew your solution was possible, but syntax was killing me. That "/text()" part in particular was not obvious even after reading specs and examples.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2016 17:21:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259304#M77739</guid>
      <dc:creator>halr9000</dc:creator>
      <dc:date>2016-07-14T17:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: parsing XML data with hierarchy of KV pairs</title>
      <link>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259305#M77740</link>
      <description>&lt;P&gt;that is so friggin cool. thanks for showing that!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2016 17:21:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/parsing-XML-data-with-hierarchy-of-KV-pairs/m-p/259305#M77740</guid>
      <dc:creator>sloshburch</dc:creator>
      <dc:date>2016-07-14T17:21:54Z</dc:date>
    </item>
  </channel>
</rss>

