Skip to content

Inconsistent type for attributes where nodes have no attributes #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TimG1964 opened this issue Feb 12, 2025 · 1 comment
Open

Inconsistent type for attributes where nodes have no attributes #33

TimG1964 opened this issue Feb 12, 2025 · 1 comment

Comments

@TimG1964
Copy link
Contributor

TimG1964 commented Feb 12, 2025

The assumption in XML.jl seems to be that if a node has no attributes then XML.attributes(node) === nothing.

For example, this constructor seems to assume this:

    function Node(nodetype::NodeType, tag=nothing, attributes=nothing, value=nothing, children=nothing)
        new(nodetype,
            isnothing(tag) ? nothing : string(tag),
            isnothing(attributes) ? nothing : OrderedDict(string(k) => string(v) for (k, v) in pairs(attributes)),
            isnothing(value) ? nothing : string(value),
            isnothing(children) ? nothing :
                children isa Node ? [children] :
                children isa Vector{Node} ? children :
                children isa Vector ? map(Node, children) :
                children isa Tuple ? map(Node, collect(children)) :
                [Node(children)]
        )
    end

and these two functions do, too:

Base.haskey(o::Node, key::AbstractString) = isnothing(o.attributes) ? false : haskey(o.attributes, key)
Base.keys(o::Node) = isnothing(o.attributes) ? () : keys(o.attributes)

The constructor XML.h() relies on the main constructor function above.

However, it is easy to create nodes with no attributes where XML.attributes(node) !== nothing.

julia> using XML

julia> n1 = XML.Element("test1")
Node (depth=1) Element <test1>

julia> XML.attributes(n1)
OrderedCollections.OrderedDict{String, String}()

julia> n2 = XML.h.test2()
Node (depth=1) Element <test2>

julia> XML.attributes(n2)
OrderedCollections.OrderedDict{String, String}()

julia> length(XML.attributes(n1))
0

julia> length(XML.attributes(n2))
0

As a consequence, nodes with no attributes may have different values for XML.attributes(node), and then the test for equality, too, will fail. This is mentioned on Discourse, here.

I have only looked at the Element constructor so far, so I don't know if the other constructors share this issue. I guess they all use the main constructor function, though.

Sorry if I've mis-understood something fundamental! 🙂

@joshday
Copy link
Member

joshday commented Mar 13, 2025

As posted in the other issue, I have a rewrite in the works that will fix this, but it'll be a few weeks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants