a XiMpLe update

meik michalke meik.michalke at uni-duesseldorf.de
Mon Nov 28 14:41:20 GMT 2022


hi,

just a few sidenotes on recent development regarding the XiMpLe package (which 
was written as a toolkit for the rkwarddev package, originally).

in the develop branch, the package now has some new features to make scripting 
and maintaining XML trees more straightforward.

the new function gen_tag_functions() generates wrapper functions around 
XMLNode() by tag name. functions go to .GlobalEnv by default, but you can also 
have them in an attached environment instead, e.g.

  attach(list(), name="XiMpLe_wrappers")
  gen_tag_functions(
    tags=c("a", "div"),
    envir=as.environment("XiMpLe_wrappers")
  )

this creates the functions a_() and div_() which are wrappers for XMLNode("a") 
and XMLNode("div"), respectively. also, node attributes can now generally be 
given as named arguments with the dots parameter, i.e. these all produce the 
same result:

  # old syntax
  XMLNode("a", "click here", attrs=list(href="foo.html"))

  # now also valid syntax
  XMLNode("a", "click here", href="foo.html")

  # new wrapper function
  a_("click here", href="foo.html")

to make this usable, i renamed the first argument of XMLNode(), which defines 
the XML node name. until now, the argument was called "name", which is also a 
commonly used attribute name, so you'd still have to use the old attrs list to 
circumvent collisions. it is now called "tag_name" and i hope this doesn't 
break anything, as the first argument is often used without explicitly writing 
it's name out...

another new feature is the "as_script" parameter in pasteXML(). instead of 
showing the object as XML code, pasteXML(..., as_script=TRUE) returns script 
code that is using the wrapper functions:

  my_node <- div_(
    a_("click here", href="foo.html")
  )

  cat(pasteXML(my_node))
  # <div>
  #   <a href="foo.html">
  #     click here
  #   </a>
  # </div>

  cat(pasteXML(my_node, as_script=TRUE))
  # div_(
  #   a_(href="foo.html",
  #     "click here"
  #   )
  # )

turning existing XML documents into R scripts should therefore be much easier 
now.

i've also tried to make the XML parser not freak out when it encounters empty 
attributes (i.e., only the attribute name without a value, as common in 
HTML5). that needs more testing. the current workaround is to add a value 
(same as attribute name), as there's still no way to generate empty attributes 
yet.


viele grüße :: m.eik

-- 
  dipl. psych. meik michalke
  institut f"ur experimentelle psychologie
  abt. f"ur diagnostik und differentielle psychologie
  heinrich-heine-universit"at d-40204 d"usseldorf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/rkward-devel/attachments/20221128/43fbcd59/attachment.sig>


More information about the rkward-devel mailing list