[NTG-context] Access current element in xmlsetsetup via xml.expressions

achim at jander.de achim at jander.de
Wed Oct 2 19:48:28 CEST 2019

Dear Hans,
that is really great. Thank's a lot.  Ill check the next beta and try to wikifiy (maybe I need further help for that)


-----Urspr├╝ngliche Nachricht-----
Von: ntg-context <ntg-context-bounces at ntg.nl> Im Auftrag von Hans Hagen
Gesendet: Mittwoch, 2. Oktober 2019 18:53
An: mailing list for ConTeXt users <ntg-context at ntg.nl>; mf <massifr at fastwebnet.it>
Betreff: Re: [NTG-context] Access current element in xmlsetsetup via xml.expressions

On 10/2/2019 12:32 PM, mf wrote:
> The answer is in lxml-lpt.lua, where built-in expressions are defined.
> You need a good knowledge of LPEG that i miss.
> Some built-in expressions get the current element as first argument, 
> like count() or child() (lines 1300-1307 of lxml-lpt.lua):
> expressions.child = function(e,pattern)
>      return applylpath(e,pattern) -- todo: cache end
> expressions.count = function(e,pattern) -- what if pattern == empty or 
> nil
>      local collected = applylpath(e,pattern) -- todo: cache
>      return pattern and (collected and #collected) or 0 end
> Some other expressions use a template that passes the "list", "ll", "l" 
> and "order" arguments you find cited in the XML manual ┬ž4.1 "Path 
> expressions - Expressions and filters".
> These are the lines 738-743 in lxml-lpt.lua:
> local template_e = [[
>      local expr = xml.expressions
>      return function(list,ll,l,order)
>          return %s
>      end
> ]]
> That template is used by the function that registers a new expression 
> (lines 807-812 in lxml-lpt.lua):
> local function register_expression(expression)
>      local converted = lpegmatch(converter,expression)
>      local runner = load(format(template_e,converted))
>      runner = (runner and runner()) or function()
> errorrunner_e(expression,converted) end
>      return { kind = "expression", expression = expression, converted 
> = converted, evaluator = runner } end
> Anyway i could not find a way to define an expression with a function 
> like this:
> xml.expressions.myexpr( ... )
> that gets access to those arguments.
> The only arguments it gets are the ones you specify in your LPATH 
> expressions; AFAIK they are attributes values -- with the @attr syntax
> -- and strings.
Live is complex isn't it? The problem, is that we also support the normal path expressions which in retrospect maybe was a bad idea ... 
better be explicit. Strings without quotes are actually intercepted as element references (just like the @ is).

So, I added a few extra accessors:

     A <inline style="sup">sup 1</inline>
     B <inline style="sub">sub 1</inline>
     C <inline style="sup">sup 2</inline>
     D <inline style="sub">sub 2</inline>
     E <inline style="sup">sup 3</inline>
     F <inline style="sup">sup 4</inline>
     G <inline style="sup">sup 5</inline>
     H <inline style="sub">sub 5</inline>
     I <inline style="sup">sup 6</inline> </para> \stopbuffer

     function xml.expressions.MyCheck(list,position,what)
      -- print("list",    list)
      -- print("position",position)
      -- print("what",    what)
         local n = list[position+1]
         return n and n.at.style == what


\startxmlsetups xml:textsetups


     % with a helper:

%     \xmlsetsetup{#1}{inline[@style='sup' and 
%     \xmlsetsetup{#1}{inline[@style='sup' and 

     % rather verbose, we *need* to use ['at'] and not .at

%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 

     % also ok

%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 

     % direct checking

%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 
%     \xmlsetsetup{#1}{inline[@style='sup' and (position() < last() and 

     % shorter

     \xmlsetsetup{#1}{inline[@style='sup' and (attribute(list()[position()+1],'style','sub'))]}{xml:supsub}
     \xmlsetsetup{#1}{inline[@style='sup' and (attribute(list()[position()+1],'style','sup'))]}{xml:supsup}


\startxmlsetups xml:para

\startxmlsetups xml:supsub

\startxmlsetups xml:supsup

\startxmlsetups xml:inline


In the next beta. Now, who is going to wikify this ...


                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context at ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net

More information about the ntg-context mailing list