<?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: Name - value pair - issue in extracting in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38932#M1835</link>
    <description>&lt;P&gt;Here is a post of the myxml.py file for the above solution.  I wasn't able to get this to format properly in the initial answer:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;
# Copyright (C) 2005-2010 Splunk Inc.  All Rights Reserved.  Version 4.0
import sys,splunk.Intersplunk
import re
import urllib
import xml.sax.saxutils as sax&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;XML_KV_RE = re.compile("&amp;lt;(.&lt;EM&gt;?)(?:\s[^&amp;gt;]&lt;/EM&gt;)?&amp;gt;([^&amp;lt;]*)")&lt;/P&gt;

&lt;P&gt;try:
    results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;for r in results:
    if "_raw" in r:
        raw = r["_raw"]
        rawOut = sax.unescape( raw )
        while( rawOut != raw ):
            raw = rawOut
            rawOut = sax.unescape( raw )                
        r["_raw"] = rawOut

        for kvpair in XML_KV_RE.findall(rawOut):
            if not kvpair[0] in r:
                r[kvpair[0]] = kvpair[1]
            else:
                r[kvpair[0]] = r[kvpair[0]] + ", " + kvpair[1]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;except:
    import traceback
    stack =  traceback.format_exc()
    results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack))&lt;/P&gt;

&lt;P&gt;splunk.Intersplunk.outputResults( results )
&lt;/P&gt;</description>
    <pubDate>Mon, 07 Feb 2011 05:04:42 GMT</pubDate>
    <dc:creator>Ron_Naken</dc:creator>
    <dc:date>2011-02-07T05:04:42Z</dc:date>
    <item>
      <title>Name - value pair - issue in extracting</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38929#M1832</link>
      <description>&lt;P&gt;Hi 
I am trying to extract the values of certain field present in Log for a particular operation:&lt;/P&gt;

&lt;P&gt;My Query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Service="X1" Operation="Y1" AND AuditType="REQUEST_OUTBOUND" | sort _time | xmlunescape | xmlkv | fields + A, B
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;There are multiple values present in the log for fields A &amp;amp; B as below:&lt;/P&gt;

&lt;P&gt;Message in Log:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;A&amp;gt;1000&amp;lt;/A&amp;gt;
&amp;lt;A&amp;gt;29&amp;lt;/A&amp;gt;
&amp;lt;A&amp;gt;30&amp;lt;/A&amp;gt;

&amp;lt;B&amp;gt;B1&amp;lt;/B&amp;gt;
&amp;lt;B&amp;gt;D2&amp;lt;/B&amp;gt;
&amp;lt;B&amp;gt;C3&amp;lt;/B&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;whereas I need my output as :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;A       B
1000    B1
29      D2
30      C3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But i am getting output result as :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;A   B
30  C3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I am getting only the last value of the name-value pair but not all the values. &lt;/P&gt;

&lt;P&gt;Could anyone please help in getting the desired output? Please let me know what command should i use and how to modify the search?&lt;/P&gt;</description>
      <pubDate>Fri, 04 Feb 2011 17:06:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38929#M1832</guid>
      <dc:creator>jenipriya</dc:creator>
      <dc:date>2011-02-04T17:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: Name - value pair - issue in extracting</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38930#M1833</link>
      <description>&lt;P&gt;Tried using "search .... | multikv fields A B" but still not getting any output. Not sure as why. Can someone please help me get this fixed.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Feb 2011 17:35:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38930#M1833</guid>
      <dc:creator>jenipriya</dc:creator>
      <dc:date>2011-02-04T17:35:50Z</dc:date>
    </item>
    <item>
      <title>Re: Name - value pair - issue in extracting</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38931#M1834</link>
      <description>&lt;P&gt;This limitation seems to stem from the xmlkv command in the search app.  It appears that the python script overwrites the previous value as it enumerates new values for the same key.&lt;/P&gt;

&lt;P&gt;Here's the culprit:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;for kvpair in XML_KV_RE.findall(rawOut):
    r[kvpair[0]] = kvpair[1]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It didn't take much time to create a "hack" to fix this, but it's not an ideal solution.  This will work:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;In $SPLUNK_HOME/etc/apps/search/bin, copy &lt;STRONG&gt;xmlkv.py&lt;/STRONG&gt; to &lt;STRONG&gt;myxml.py&lt;/STRONG&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;In &lt;STRONG&gt;myxml.py&lt;/STRONG&gt;, replace the code above with the following, but be very careful to indent the code properly.  The indentations don't display properly on this page, so you will have to eyeball them according to the rest of the code in myxml.py.  Everything under the "for kvpair" line needs to be indented, and the lines under "if not" and under "else:" both need to be further indented.  If you don't indent properly, you'll probably just generate an error in the Splunk UI when you run the command we're building.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    for kvpair in XML_KV_RE.findall(rawOut):
if not kvpair[0] in r:
            r[kvpair[0]] = kvpair[1]
else:
    r[kvpair[0]] = r[kvpair[0]] + ", " + kvpair[1]
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Create/edit $SPLUNK_HOME/etc/apps/search/local/commands.conf and add the following:&lt;/P&gt;

&lt;P&gt;[myxml]&lt;BR /&gt;
filename = myxml.py&lt;BR /&gt;
retainsevents = true&lt;BR /&gt;
overrides_timeorder = false  &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Restart Splunk&lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;We have just created a new command, &lt;STRONG&gt;myxml&lt;/STRONG&gt;, that you can use in place of &lt;STRONG&gt;xmlkv&lt;/STRONG&gt; to collect the multivalue XML fields.  The multivalue XML fields will now show up as comma-separated values.  For instance, A = "1000, 29, 30" and "B = B1, D2, C3".  If you need to split the fields into individual values, you can use &lt;STRONG&gt;makemv&lt;/STRONG&gt;.&lt;/P&gt;

&lt;P&gt;Here is a sample search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=xmlkv | myxml | makemv delim="," A | makemv delim="," B
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;When I perform this in the lab, I see A(3) and B(3) in the Field Picker, and I can perform operations on any of the individual values.&lt;/P&gt;

&lt;P&gt;HTH&lt;BR /&gt;
ron&lt;/P&gt;</description>
      <pubDate>Sun, 06 Feb 2011 07:53:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38931#M1834</guid>
      <dc:creator>Ron_Naken</dc:creator>
      <dc:date>2011-02-06T07:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: Name - value pair - issue in extracting</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38932#M1835</link>
      <description>&lt;P&gt;Here is a post of the myxml.py file for the above solution.  I wasn't able to get this to format properly in the initial answer:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;
# Copyright (C) 2005-2010 Splunk Inc.  All Rights Reserved.  Version 4.0
import sys,splunk.Intersplunk
import re
import urllib
import xml.sax.saxutils as sax&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;XML_KV_RE = re.compile("&amp;lt;(.&lt;EM&gt;?)(?:\s[^&amp;gt;]&lt;/EM&gt;)?&amp;gt;([^&amp;lt;]*)")&lt;/P&gt;

&lt;P&gt;try:
    results,dummyresults,settings = splunk.Intersplunk.getOrganizedResults()&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;for r in results:
    if "_raw" in r:
        raw = r["_raw"]
        rawOut = sax.unescape( raw )
        while( rawOut != raw ):
            raw = rawOut
            rawOut = sax.unescape( raw )                
        r["_raw"] = rawOut

        for kvpair in XML_KV_RE.findall(rawOut):
            if not kvpair[0] in r:
                r[kvpair[0]] = kvpair[1]
            else:
                r[kvpair[0]] = r[kvpair[0]] + ", " + kvpair[1]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;except:
    import traceback
    stack =  traceback.format_exc()
    results = splunk.Intersplunk.generateErrorResults("Error : Traceback: " + str(stack))&lt;/P&gt;

&lt;P&gt;splunk.Intersplunk.outputResults( results )
&lt;/P&gt;</description>
      <pubDate>Mon, 07 Feb 2011 05:04:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38932#M1835</guid>
      <dc:creator>Ron_Naken</dc:creator>
      <dc:date>2011-02-07T05:04:42Z</dc:date>
    </item>
    <item>
      <title>Re: Name - value pair - issue in extracting</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38933#M1836</link>
      <description>&lt;P&gt;Hi Ron &lt;/P&gt;

&lt;P&gt;It did work, and thanks a lot for your reply.&lt;/P&gt;

&lt;P&gt;My sincere apologies for the late response, as i have some n/w connectivity issues and couldn't sign in for a long time.&lt;/P&gt;

&lt;P&gt;Once again thanks for your valuable inputs.&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;Jeni&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 18 Feb 2011 17:05:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Name-value-pair-issue-in-extracting/m-p/38933#M1836</guid>
      <dc:creator>jenipriya</dc:creator>
      <dc:date>2011-02-18T17:05:57Z</dc:date>
    </item>
  </channel>
</rss>

