# Extract multiple values from XML logs where field names may vary

**URL:** <https://community.dynatrace.com/t/extract-multiple-values-from-xml-logs-where-field-names-may-vary/54016>\
**Category:** DQL\
**Tags:** dql, dpl, logs\
**Created:** [November 7, 2025, 8:32am UTC](https://community.dynatrace.com/t/extract-multiple-values-from-xml-logs-where-field-names-may-vary/54016 "2025-11-07T08:32:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kajao](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kajao](https://community.dynatrace.com/u/Kajao)\
**Post date:** [November 7, 2025, 8:32am UTC](https://community.dynatrace.com/t/extract-multiple-values-from-xml-logs-where-field-names-may-vary/54016/1 "2025-11-07T08:32:35Z")

</div>

I have XML-based logs from an API integration that look like this (simplified example):

```markup
2025-11-03 09:00:01,445 lLv="INFO" [<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
 <soapenv:Header>
   <ns1:SourceApplication>myApp</ns1:SourceApplication>
   <ns1:Function>Payment</ns1:Function>
 </soapenv:Header>
 <soapenv:Body>
  <PaymentInitiateApproveUpdateRequest>
   <PaymentInitiateApproveRequest>
     <ns8:TransactionNumber xmlns:ns8="x:x">111111</ns8:TransactionNumber>
     <ns9:SerialNumber xmlns:ns9="x:x">22222222</ns9:SerialNumber>
   </PaymentInitiateApproveRequest>
   <PaymentInitiateApproveRequest>
     <ns19:TransactionNumber xmlns:ns19="x:x">333333</ns19:TransactionNumber>
     <ns20:SerialNumber xmlns:ns20="x:x">44444444</ns20:SerialNumber>
   </PaymentInitiateApproveRequest>
  </PaymentInitiateApproveUpdateRequest>
 </soapenv:Body>
</soapenv:Envelope>], response: [<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <ns4:PaymentInitiateApproveUpdateResponse 
   <ns5:ResponseState>
    <ErrorCode>0</ErrorCode>
   </ns5:ResponseState>
   <ns4:PaymentInitiateApproveResponse>
    <ns2:PaymentInitiateKey>
     <ns3:TransactionNumber>555555</ns3:TransactionNumber>
     <ns3:SerialNumber>66666666</ns3:SerialNumber>
    </ns2:PaymentInitiateKey>
    <ns2:ServiceElementResult>
     <ns2:ErrorCode>0</ns2:ErrorCode>
    </ns2:ServiceElementResult>
   </ns4:PaymentInitiateApproveResponse>
   <ns4:PaymentInitiateApproveResponse>
    <ns2:PaymentInitiateKey>
     <ns3:TransactionNumber>777777</ns3:TransactionNumber>
     <ns3:SerialNumber>888888888</ns3:SerialNumber>
    </ns2:PaymentInitiateKey>
    <ns2:ServiceElementResult>
     <ns2:ErrorCode>0</ns2:ErrorCode>
    </ns2:ServiceElementResult>
   </ns4:PaymentInitiateApproveResponse>
  </ns4:PaymentInitiateApproveUpdateResponse>
 </soap:Body>
</soap:Envelope>]."

```

I need to extract fields like:

- **Function** from the header.
- **TransactionNumber** and **SerialNumber** from the request.
- **ErrorCode** from the response (there can be multiple for payments and one for transfer)

Challenges\*\*:\*\*

- These elements can appear multiple times (arrays of PaymentInitiateApproveRequest and PaymentInitiateApproveResponse).
- The XML tags vary, for example SerialNumber might appear as:
  - ns9:SerialNumber614276066\</ns9:SerialNumber\>
  - ns1:SerialNumber614276066\</ns1:SerialNumber\>
  - \<SerialNumber\>614276066\</SerialNumber\>

- My main problem is extracting the ErrorCode from the payment. It always appears after ns2:ServiceElementResult:

```markup
<ns2:ServiceElementResult>
 <ns2:ErrorCode>0</ns2:ErrorCode>

```

This approach work for all the fields except the _payment_ ErrorCode:

```python
| parse content, """Array{DATA 'TransactionNumbe' LD '>' LONG*:i '</' LD 'ransactionNumber>'LF}{1,}:trans_num """
| parse content, """Array{DATA 'SerialNumbe' LD '>' LONG*:i '</' LD 'erialNumber>'LF}{1,}:ser_num """
| parse content, """DATA "ErrorCode>" DATA:error_code_transfer "</ErrorCode>" """
| parse content, """Array{DATA 'ServiceElementResult>' EOL LD 'ErrorCode>' LONG*:i '</' LD 'ErrorCode>' LF}{1,}:error_code_payment """ // does not work

```

I have also tried with the XML approach:

```python
| parse content, """ DATA '[' XML_PLAIN:xml_env ']' DATA '[' XML_PLAIN:xml_response """
| fieldsAdd env_header = xml_env[`soapenv:Envelope`][`soapenv:Header`], reponse_body = xml_response[`soap:Envelope`][`soap:Body`]

```

But I am not able to handle&nbsp;varying tag names (namespace prefixes) and multiple values with this approach.

---

<div class="post-metadata">

**Author:** ![Tiit\_Hallas](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@Tiit\_Hallas](https://community.dynatrace.com/u/Tiit_Hallas)\
**Post date:** [November 18, 2025, 10:39pm UTC](https://community.dynatrace.com/t/extract-multiple-values-from-xml-logs-where-field-names-may-vary/54016/2 "2025-11-18T22:39:53Z")

</div>

I would use the following approach (ignoring namespaces and dealing with multiple values):

```markup
| parse content, "LD XML_PLAIN(ignoreNamespace=true):request ld '[' XML(ignoreNamespace=true):response"

```

Additionally you can use the **expand** and **fieldsFlatten** commands to open up the records and arrays to separate result records. A solution can be seen in Security Investigator on our Playground at&nbsp;[Dynatrace](https://wkf10640.apps.dynatrace.com/ui/apps/dynatrace.security.investigator/share/9bff51ab-29fd-472e-a96d-49ce58453d60)

 ![Tiit_Hallas_0-1763505584043.png](https://global.discourse-cdn.com/dynatrace/original/2X/3/31b7b3645c642dadb6e2db35fab1be2da9187a1c.png)
