# Traverse through XML document

**URL:** <https://discuss.elastic.co/t/traverse-through-xml-document/194967>\
**Category:** Logstash\
**Created:** [August 13, 2019, 6:51am UTC](https://discuss.elastic.co/t/traverse-through-xml-document/194967 "2019-08-13T06:51:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvenkat\_in](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mvenkat\_in](https://discuss.elastic.co/u/mvenkat_in)\
**Post date:** [August 13, 2019, 6:51am UTC](https://discuss.elastic.co/t/traverse-through-xml-document/194967/1 "2019-08-13T06:51:17Z")

</div>

Hi  
I have the below XML structure with a list of cartItem (it can be N and not definite in number). I want help for the below queries

1 - I want to traverse through each of the cartItem and check the conditions based on cartItem.action & if the condition matches then access cartItem.externalProductId of that particular cartItem node.

2 - How to write the Sample XML filter in order to do the above and also in general to parse this entire XML and access each of the elements like cartItems & Cart.cartNumber ..etc.

```
<SOAP-ENV:Envelope>
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
	<ns5:Submit_REQ>
		<ns5:Body>
			<ns5:Cart>
				<ns5:cartNumber></ns5:cartNumber>
				<ns5:createdDate></ns5:createdDate>
				<ns5:price></ns5:price>
				<ns5:cartItemList>
					<ns5:cartItem>
						<ns5:id>1</ns5:id>
						<ns5:externalProductId></ns5:externalProductId>
						<ns5:CheckoutPrice></ns5:CheckoutPrice>
						<ns5:action></ns5:action>
					</ns5:cartItem>
					<ns5:cartItem>
						<ns5:id>2</ns5:id>
						<ns5:externalProductId></ns5:externalProductId>
						<ns5:CheckoutPrice></ns5:CheckoutPrice>
						<ns5:action></ns5:action>
					</ns5:cartItem>
				<ns5:cartItem>
						<ns5:id>N</ns5:id>
						<ns5:externalProductId></ns5:externalProductId>
						<ns5:CheckoutPrice></ns5:CheckoutPrice>
						<ns5:action></ns5:action>
					</ns5:cartItem>						
				</ns5:cartItemList>
			</ns5:Cart>
		</ns5:Body>
	</ns5:Submit_REQ>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 13, 2019, 2:30pm UTC](https://discuss.elastic.co/t/traverse-through-xml-document/194967/2 "2019-08-13T14:30:55Z")

</div>

You can parse it with an XML filter. I cannot get the namespaces option to work and I am drawing a blank on why, so I just strip the namespaces. To process a variable length array you will need a ruby filter.

```
    mutate { gsub => ["message", "</?SOAP-ENV:[^>]+>", "" ] }
    mutate { gsub => ["message", "ns5:", ""] }
    xml { source => "message" target => "theXML" force_array => false remove_field => ["message"] }
    ruby {
        code => '
            c = event.get("[theXML][Body][Cart][cartItemList][cartItem]")
            if c
                c.each_index { |x|
                    # Do something with c[x]
                }
            end
        '
    }
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 10, 2019, 2:30pm UTC](https://discuss.elastic.co/t/traverse-through-xml-document/194967/3 "2019-09-10T14:30:59Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
