LightXML.jl icon indicating copy to clipboard operation
LightXML.jl copied to clipboard

New versions of create_root, new_child, etc with attributes and inner text

Open 315234 opened this issue 9 years ago • 7 comments

This is a feature request to add methods for creating new nodes with an attribute dictionary, like this:

function create_root( xdoc::XMLDocument
                    , name::AbstractString
                    , attrs::Dict{ASCIIString,ASCIIString} )
    xroot = create_root(xdoc, name)
    set_attributes(xroot, attrs)
    xroot
end
function new_child( parent::XMLElement
                  , name::AbstractString
                  , attrs::Dict{ASCIIString,ASCIIString} )
    child = new_child(parent, name)
    set_attributes(child, attrs)
    child
end
function new_child( parent::XMLElement
                  , name::AbstractString
                  , attrs::Dict{ASCIIString,ASCIIString}
                  , text::ASCIIString )
    child = new_child2(parent, name, attrs)
    add_text(child, text)
    child
end

This would remove the need for additional calls to set_attributes or add_text and make the creation of new elements much more streamlined, for example:

xgeom = new_child(xgrid, "Geometry", Dict("Type"=>"VXVYVZ"))
xxdat = new_child(xgeom, "DataItem", Dict("Dimensions"=>"4 2", "Format"=>"HDF", "Precision"=>"8"), xspacedata)

315234 avatar Oct 20 '15 08:10 315234

Could be additional methods using keyword arguments, I suppose. I'd look over a pull request.

tkelman avatar Oct 20 '15 08:10 tkelman

Would there be an issue with having them defined as methods with positional arguments, rather than keyword arguments, and letting dispatch choose the appropriate one?

I will try and submit a pull request if I can figure out how AbstractString and friends are actually supposed to work, and if I can get it working for both 0.3 and 0.4.

315234 avatar Oct 20 '15 11:10 315234

The issue with positional arguments is whether the particular order is a clear API. Sometimes it's clearer to have named inputs. For example with attributes and text, what if you want to add text but not have any attributes? I also lean towards separating functionality out into smaller, more composable pieces, so I'm not sure what trying to put everything into the same list of arguments for a single function call accomplishes.

tkelman avatar Oct 20 '15 11:10 tkelman

Wouldn't it make sense to pass all attributes as keyword arguments, like this?

function create_root(xdoc::XMLDocument,
                     name::AbstractString; # Notice the semicolon
                     attrs...)

@315234 See Compat.jl to get things working on both 0.3 and 0.4.

nalimilan avatar Oct 20 '15 11:10 nalimilan

That would only work for assigning to attributes whose names are valid Julia identifiers (so no keywords, no punctuation), but otherwise would be consistent with one of the signatures of set_attributes

tkelman avatar Oct 20 '15 11:10 tkelman

Right. There could be an alternative method taking a Dict{AbstractString, AbstractString} for corner cases. (At least it could be added later if somebody requests it.)

nalimilan avatar Oct 20 '15 11:10 nalimilan

Actually, doing f(symbol("/")=>1) works.

nalimilan avatar Oct 20 '15 12:10 nalimilan