# Testing XML.Nodes for equality

**URL:** <https://discourse.julialang.org/t/testing-xml-nodes-for-equality/125800>\
**Category:** General Usage\
**Tags:** question, xml\
**Created:** [February 11, 2025, 5:50pm UTC](https://discourse.julialang.org/t/testing-xml-nodes-for-equality/125800 "2025-02-11T17:50:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [February 11, 2025, 5:50pm UTC](https://discourse.julialang.org/t/testing-xml-nodes-for-equality/125800/1 "2025-02-11T17:50:16Z")

</div>

I’m having trouble making the test for equality between `XML.node`s work. XML.jl defines a `nodes_equal` function to test for equality, but I can’t make it work reliably.

For example, I have two `XML.node`s called `node` and `new_node`. If I compare all the attributes tested in the function for equality like this:

```julia
        println(XML.write(node))
        println(XML.write(new_font))
        println(XML.nodes_equal(node, new_font))
        println("tags : ", XML.tag(node) == XML.tag(new_font))
        println("types : ", XML.nodetype(node) == XML.nodetype(new_font))
        println("attributes : ", XML.attributes(node) == XML.attributes(new_font))
        println(XML.attributes(node))
        println(XML.attributes(new_font))
        println("values : ", XML.value(node) == XML.value(new_font))
        println("length : ", length(XML.children(node)) == length(XML.children(new_font)))
        println("children : ", all(XML.nodes_equal(ai, bi) for (ai,bi) in zip(XML.children(node), XML.children(new_font))))

```

I get the following results:

```julia
<font> # This is `node`
  <b/>
  <sz val="18"/>
  <color theme="1"/>
  <name val="Calibri"/>
  <family val="2"/>
  <scheme val="minor"/>
</font>
<font> # This is `new_font`
  <b/>
  <sz val="18"/>
  <color theme="1"/>
  <name val="Calibri"/>
  <family val="2"/>
  <scheme val="minor"/>
</font>
false # They look the same, but they are not!
tags : true
types : true
attributes : false
nothing # This is the source of the difference!
OrderedCollections.OrderedDict{String, String}()
values : true
length : true
children : false

```

The two parent `<font.../>` nodes have no attributes, but in one this is shown as `XML.attributes(node) == nothing` while in the other, it results in `XML.attributes(new_font) == OrderedCollections.OrderedDict{String, String}()`. This difference isn’t a result of anything I have done intentionally. How can I avoid it?

I suspect that the source of the differences in the children is the same but haven’t investigated.

The first node is read from an XML file. The second `node` is constructed this way:

```julia
const font_tags = ["b", "i", "u", "strike", "outline", "shadow", "condense", "extend", "sz", "color", "name", "family", "scheme"]

function buildNode(tag::String, attributes::Dict{String, Union{Nothing, Dict{String, String}}}) :: XML.Node
    new_node = XML.Element(tag)
    for a in font_tags # Use this as a device to keep ordering constant
        if haskey(attributes, a)
            n=XML.Element(a)
            if !isnothing(attributes[a])
                for (k, v) in attributes[a]
                    n[k] = v
                end
            end
            push!(new_node, n)
        end
    end
    return new_node
end

```

and perhaps this is giving rise to the difference somehow.

How can I avoid this issue?

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [February 12, 2025, 8:48am UTC](https://discourse.julialang.org/t/testing-xml-nodes-for-equality/125800/2 "2025-02-12T08:48:31Z")

</div>

Well, one way to avoid the issue is like this:

```julia
XML.parse(XML.Node, XML.write(node)) == XML.parse(XML.Node, XML.write(new_font))

```

but this isn’t exactly elegant. Additionally, it forces the `XML.nodetype` to be `Document`, thus removing one element of the comparison.

Does anyone have a better solution? If not, I think the problem/fix lies with XML.jl.

---

<div class="post-metadata">

**Author:** ![TimG](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@TimG](https://discourse.julialang.org/u/TimG)\
**Post date:** [February 12, 2025, 11:06am UTC](https://discourse.julialang.org/t/testing-xml-nodes-for-equality/125800/3 "2025-02-12T11:06:10Z")

</div>

I have created an XML.jl issue [here](https://github.com/JuliaComputing/XML.jl/issues/33) and a (somewhat speculative) PR [here](https://github.com/JuliaComputing/XML.jl/pull/34)
