# XML node element extraction based on context

**URL:** <https://discourse.julialang.org/t/xml-node-element-extraction-based-on-context/14565>\
**Category:** General Usage\
**Created:** [September 5, 2018, 4:31am UTC](https://discourse.julialang.org/t/xml-node-element-extraction-based-on-context/14565 "2018-09-05T04:31:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [September 5, 2018, 4:31am UTC](https://discourse.julialang.org/t/xml-node-element-extraction-based-on-context/14565/1 "2018-09-05T04:31:06Z")

</div>

Hello,  
I am using LightXML to extract element nodes which pertain to musical parameters. I push the valuable elements to an array and then to a text file. Currently, I am struggling with how to exclude/include certain node elements based on their context.

For instance, I want to extract all durations that have this context:

```julia
</pitch>
<duration>1024</duration>

```

But I don’t want to extract durations that have these contexts.

```julia
<backup>
<duration>1024</duration>
</backup>

<rest />
<duration>1024</duration>

```

Thank you very much!  
Nakul

---

<div class="post-metadata">

**Author:** ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)\
**Post date:** [September 5, 2018, 5:44am UTC](https://discourse.julialang.org/t/xml-node-element-extraction-based-on-context/14565/2 "2018-09-05T05:44:11Z")

</div>

I’m not sure what you mean by “context”, but if it can be described in [XPath](https://www.w3schools.com/xml/xpath_intro.asp), my sweet [EzXML.jl](https://github.com/bicycle1885/EzXML.jl) package is very helpful. It defines `findall(xpath, node)` that returns all XML nodes matching `xpath` under `node`.

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [September 5, 2018, 5:53am UTC](https://discourse.julialang.org/t/xml-node-element-extraction-based-on-context/14565/4 "2018-09-05T05:53:54Z")

</div>

I am examining EzXML right now. I’ve never heard of XPath but it seems very promising.

To elaborate on my specific needs and the context criteria: I want to only collect duration node elements which are preceded by pitch node elements. Sometimes, duration node elements are preceded by backup nodes or rest nodes, which I want to exclude from my collection. Is there an XPath query for that?

Thanks!
