[NTG-context] XML expression to read HTML classes

massifr at fastwebnet.it massifr at fastwebnet.it
Tue Aug 25 17:05:23 CEST 2015

Hello list,
I'm working on HTML typesetting with ConTeXt.
I wrote a custom expression to test whether an element has a class:

function xml.functions.classes(classAttr)
  local classes = {}
  if classAttr then
    for c in string.gmatch(classAttr, "%S+") do
      if (string.len(c) > 0) then classes[c] = true end
  return classes

function xml.expressions.hasClass(classAttr, className)
  if classAttr and className then
    return xml.functions.classes(classAttr)[className] ~= nil
  return false

This way I can write this in my xmlsetup:

That is better than 
- p[@class='myclass1']   (it fails when you have more classes)
- p[contains(@class, 'myclass1')]    (it gives you false positives when you have class="myclass12")

That works, but I would like to write:

omitting the @class argument, because it's obvious.
To omit it I need to access the current collected element.

At page 25 of "Dealing with XML in ConTeXt MkIV" it is said 
that the predefined variables "list","l","ll" (I think I need that one)
and "order" are available, but I did not manage to access them.

How can I get the current element in Lua to write
local classAttr = xml.attribute(currentElement, "", "class")   ?

Thanks in advance,

More information about the ntg-context mailing list