Dashboards & Visualizations

Extracting attributes from XML Schema

amanno
New Member

I know there has to be a nice way to do this upon indexing in Splunk...I have a large XML file that I am indexing on the tag info however when each event is index I want some information from higher up the "tree" of the XML to be placed into that event. For a small example in:

 <Baseball>
         <player name="freese" number="23">
             <stats>
                       <info index=0>
                           <Team>Pittsburgh Pirates</Team>
                           <BA>.249</BA>
                       </info>
                       <info index=1>
                          <Team>Cardinals</Team>
                          <BA>.248</BA>
            </stats>
        </player>
         <player name="Pujols" number="5">
             <stats>
                       <info index=0>
                           <Team>LA Angles</Team>
                           <BA>.236</BA>
                       </info>
                       <info index=1>
                          <Team>Cardinals</Team>
                          <BA>.307</BA>
            </stats>
        </player>
    </Baseball>

I would like to have events that have all the information in the info tag as well as the two attributes of player name and number. Any ideas would be greatly appreciated.

Tags (2)
0 Karma

niketn
Legend

First off, your sample XML seem to have info end tags missing. You can use spath or xpath command to extract fields from XML. Based on the your needs you can extract individual field or as multi valued fields.

| makeresults 
| eval xml_data="<Baseball>
  <player name=\"freese\" number=\"23\">
      <stats>
        <info index=0>
            <Team>Pittsburgh Pirates</Team>
            <BA>.249</BA>
        </info>
        <info index=1>
           <Team>Cardinals</Team>
           <BA>.248</BA>
        </info>
     </stats>
  </player>
  <player name=\"Pujols\" number=\"5\">
    <stats>
        <info index=0>
            <Team>LA Angles</Team>
            <BA>.236</BA>
        </info>
        <info index=1>
           <Team>Cardinals</Team>
           <BA>.307</BA>
        </info>
    </stats>
  </player>
 </Baseball>"
 | spath input=xml_data path=Baseball.player{1}{@name} output=name
 | spath input=xml_data path=Baseball.player{1}{@number} output=number
 | spath input=xml_data path=Baseball.player{@name} output=names
 | spath input=xml_data path=Baseball.player{@number} output=numbers
 | spath input=xml_data path=Baseball.player.stats.info.Team output=Team
 | spath input=xml_data path=Baseball.player.stats.info.BA output=BA
 | table xml_data name names number numbers Team BA

Refer to Splunk documentation for more details: http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...