Re: [NTG-context] Request for xml.filter explanation!

2019-04-11 Thread Hans Hagen

On 4/10/2019 9:27 PM, Geert Verhaag wrote:

Hi,

I've the following xml file:

*


  
   a paragraph of text
   40.2
  
  
   another paragraph of text
   10.7
  
  
   still another paragraph!
   20.5
  
*

And want to calculate the total value of all the objects:

like so:

*\startxmlsetups xml:example:base
   \xmlsetsetup{#1}{content|object}{xml:example:*}
\stopxmlsetups

\xmlregisterdocumentsetup{example}{xml:example:base}

\startxmlsetups xml:example:content
totalvalue: \xmlfunction{#1}{totalvalue}
\xmlflush{#1} \endgraf
\stopxmlsetups

\startxmlsetups xml:example:object
attribute : \xmlatt{#1}{mid} \par
\stopxmlsetups

% LUA

\startluacode
function xml.functions.totalvalue(root)
local score = 0
for e in xml.collected(root,"/object") do
tex.write('Okay!')
--score = score + xml.filter(e,"xml:///value/number()") or 0
end
tex.write(score)
end
\stopluacode

\starttext

\xmlprocessfile{example}{content.xml}{}

\stoptext*

*
*

When I run this example I get:

*totalvalue: Okay!Okay!Okay!0 attribute : a
attribute : b
attribute : c*

That looks allright!

But when I incorporate the line:

*score = score + xml.filter(e,"xml:///value/number()") or 0*

the following Lua error is thrown:
**

*! LuaTeX error [string "\directlua "]:5: attempt to perform arithmetic 
on a nil value*


Has anybody any idea what I'm doing wrong here? The manual 
*xml-mkiv.pdf* doesn't give any clue about this!

\startluacode
function xml.functions.totalvalue(root)
local score = 0
for e in xml.collected(root,"/object/value") do
score = score + tonumber(xml.text(e)) or 0
end
context(score)
end
\stopluacode


-
  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@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
___


[NTG-context] Request for xml.filter explanation!

2019-04-10 Thread Geert Verhaag

Hi,

I've the following xml file:

*


 
  a paragraph of text
  40.2
 
 
  another paragraph of text
  10.7
 
 
  still another paragraph!
  20.5
 
*

And want to calculate the total value of all the objects:

like so:

*\startxmlsetups xml:example:base
  \xmlsetsetup{#1}{content|object}{xml:example:*}
\stopxmlsetups

\xmlregisterdocumentsetup{example}{xml:example:base}

\startxmlsetups xml:example:content
totalvalue: \xmlfunction{#1}{totalvalue}
\xmlflush{#1} \endgraf
\stopxmlsetups

\startxmlsetups xml:example:object
attribute : \xmlatt{#1}{mid} \par
\stopxmlsetups

% LUA

\startluacode
function xml.functions.totalvalue(root)
local score = 0
for e in xml.collected(root,"/object") do
tex.write('Okay!')
--score = score + xml.filter(e,"xml:///value/number()") or 0
end
tex.write(score)
end
\stopluacode

\starttext

\xmlprocessfile{example}{content.xml}{}

\stoptext*

*
*

When I run this example I get:

*totalvalue: Okay!Okay!Okay!0 attribute : a
attribute : b
attribute : c*

That looks allright!

But when I incorporate the line:

*score = score + xml.filter(e,"xml:///value/number()") or 0*

the following Lua error is thrown:
**

*! LuaTeX error [string "\directlua "]:5: attempt to perform arithmetic 
on a nil value*


Has anybody any idea what I'm doing wrong here? The manual 
*xml-mkiv.pdf* doesn't give any clue about this!


Regards,
Gerard
--


___
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@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
___