Dashboards & Visualizations

how extract fields from XML maintaining alignment if same tags are repeated?

marcoemme41
New Member

I have an XML with some tags repetead and other tags not repeated. I need extract all fields from this XML maintaining corrispondence between all tags.

I can explain using the example reported in Splunk docs for spath command (https://docs.splunk.com/Documentation/Splunk/7.2.4/SearchReference/Spath):

This is my XML and the query to extract fields:
alt text

The output of this query is:
alt text

This is wrong because "Clarke" is associated to "A Clash of Kings" instead to "Jonathan Strange".
What I need, instead, is Martin repeated in the first and second lines of Author column, and Clarke written in third line.

Someone can help me?

Tags (2)
0 Karma

niketn
Legend

@marcoemme41, ideally if you are interested in <book> being the root node of individual events and not <purchases> you should try to index only <book> node and discard <publisher> node by pushing to null queue:
props.conf

BREAK_ONLY_BEFORE=\<book\>
MUST_BREAK_AFTER=\<\/book\>
KV_MODE=xml
TRANSFORMS-nullQueueDiscardPurchases=nullQueueDiscardPurchases

transforms.conf

[nullQueueDiscardPurchases]
REGEX = purchases
DEST_KEY = queue
FORMAT = nullQueue

However, as per your question following is searchtime. Consider this only as workaround, since, ideal way would to ingest the data in proper way as expected.

| makeresults
| eval _raw="
<purchases>
  <book>
    <author>Martin</author>
    <title>A Game of Thrones</title>
    <title>A Clash of Kings</title>
  </book>
  <book>
    <author>Clarke</author>
    <title>Jonathan Strange</title>
  </book>
</purchases>"
| spath input=_raw path=purchases
| eval purchases=replace(purchases,"</book>","</book>|")
| makemv purchases delim="|"
| mvexpand purchases
| rename purchases as _raw
| spath
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Get Updates on the Splunk Community!

Index This | What is broken 80% of the time by February?

December 2025 Edition   Hayyy Splunk Education Enthusiasts and the Eternally Curious!    We’re back with this ...

Unlock Faster Time-to-Value on Edge and Ingest Processor with New SPL2 Pipeline ...

Hello Splunk Community,   We're thrilled to share an exciting update that will help you manage your data more ...

Splunk MCP & Agentic AI: Machine Data Without Limits

Discover how the Splunk Model Context Protocol (MCP) Server can revolutionize the way your organization uses ...