xmldoif: checking for contents of attribute
Hi, I'm trying to check whether an attribute contains (or, actually starts with) a given string. I've tried a whole bunch of different combinations, but I've had no luck so far. Minimal example below. Anyone has a hint? Best, Denis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \startxmlsetups xml:test \xmlsetsetup{#1}{*}{-} \xmlsetsetup{#1}{doc|element}{xml:*} \stopxmlsetups \xmlregisterdocumentsetup{test}{xml:test} \startxmlsetups xml:doc \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:element \xmlfilter{#1}{./find(attribute('class'), 'abc')/command(xml:whatever)} \xmldoifelse{#1}{./attribute('class')/contains('abc')} {Yes}{No} \stopxmlsetups \startxmlsetups xml:whatever Yes \stopxmlsetups \startbuffer[test] <?xml version="1.0" encoding="UTF-8"?> <doc> <element class="abcdefg">Yes</element> <element>No</element> </doc> \stopbuffer \starttext \xmlprocessbuffer{test}{test}{} \stoptext
One way would be to do the search in Lua; when you have multiple searches to perform, that may be the easiest way to go. For example: \startxmlsetups xml:test \xmlsetsetup{#1}{*}{-} \xmlsetsetup{#1}{doc|element}{xml:*} \stopxmlsetups \xmlregisterdocumentsetup{test}{xml:test} \startxmlsetups xml:doc \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:element \xmlfunction {#1} {element} \stopxmlsetups \startluacode function xml.functions.element (t) if t.at.class and t.at.class:find ("abc") then context.startcolor { "blue" } lxml.flush (t) context.stopcolor () else context.startcolor { "red" } lxml.flush (t) context.stopcolor () end end \stopluacode \startbuffer[test] <?xml version="1.0" encoding="UTF-8"?> <doc> <element class="abcdefg">Yes</element> <element>No</element> </doc> \stopbuffer \starttext \xmlprocessbuffer{test}{test}{} \stoptext Hope that gets you started. Thomas
On 24. Aug 2023, at 16:11, denis.maier@unibe.ch wrote:
Hi, I’m trying to check whether an attribute contains (or, actually starts with) a given string. I’ve tried a whole bunch of different combinations, but I’ve had no luck so far. Minimal example below. Anyone has a hint? Best, Denis %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \startxmlsetups xml:test \xmlsetsetup{#1}{*}{-} \xmlsetsetup{#1}{doc|element}{xml:*} \stopxmlsetups \xmlregisterdocumentsetup{test}{xml:test} \startxmlsetups xml:doc \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:element \xmlfilter{#1}{./find(attribute('class'), 'abc')/command(xml:whatever)} \xmldoifelse{#1}{./attribute('class')/contains('abc')} {Yes}{No} \stopxmlsetups \startxmlsetups xml:whatever Yes \stopxmlsetups \startbuffer[test] <?xml version="1.0" encoding="UTF-8"?> <doc> <element class="abcdefg">Yes</element> <element>No</element> </doc> \stopbuffer \starttext \xmlprocessbuffer{test}{test}{} \stoptext
-----Ursprüngliche Nachricht----- Von: Thomas A. Schmitz
Gesendet: Donnerstag, 24. August 2023 16:35 An: mailing list for ConTeXt users Betreff: [NTG-context] Re: xmldoif: checking for contents of attribute One way would be to do the search in Lua; when you have multiple searches to perform, that may be the easiest way to go. For example:
...
Hope that gets you started.
Thomas
Thank you, Thomas. Yes, that get's me started. But same question applies this solution too: Why do you need to double check? (I guess the answer will probably be the same.) Anyway, it looks like I should really look more into Lua as this will probably make a few things much easier. Best, Denis
On 8/24/2023 4:11 PM, denis.maier@unibe.ch wrote:
Hi,
I'm trying to check whether an attribute contains (or, actually starts with) a given string. I've tried a whole bunch of different combinations, but I've had no luck so far. Minimal example below. Anyone has a hint?
Best, Denis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \startxmlsetups xml:test \xmlsetsetup{#1}{*}{-} \xmlsetsetup{#1}{doc|element}{xml:*} \stopxmlsetups
\xmlregisterdocumentsetup{test}{xml:test}
\startxmlsetups xml:doc \xmlflush{#1} \stopxmlsetups
\startxmlsetups xml:element \xmlfilter{#1}{./find(attribute('class'), 'abc')/command(xml:whatever)}
\xmldoifelse{#1}{./attribute('class')/contains('abc')} {Yes}{No} \stopxmlsetups
\startxmlsetups xml:whatever Yes \stopxmlsetups
\startbuffer[test] <?xml version="1.0" encoding="UTF-8"?> <doc> <element class="abcdefg">Yes</element> <element>No</element> </doc> \stopbuffer
\starttext
\xmlprocessbuffer{test}{test}{}
\stoptext \xmldoifelse{#1}{.[@class and contains(@class,'abc')]}{Yes}{No} \xmldoifelse{#1}{.[@class and find(@class,"abc")]}{Yes}{No}
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
-----Ursprüngliche Nachricht----- Von: Hans Hagen
Gesendet: Donnerstag, 24. August 2023 16:54 An: ntg-context@ntg.nl Betreff: [NTG-context] Re: xmldoif: checking for contents of attribute On 8/24/2023 4:11 PM, denis.maier@unibe.ch wrote:
Hi,
I'm trying to check whether an attribute contains (or, actually starts with) a given string.
\xmldoifelse{#1}{.[@class and contains(@class,'abc')]}{Yes}{No} \xmldoifelse{#1}{.[@class and find(@class,"abc")]}{Yes}{No}
Thanks for your help. I can use that, but why is this sort of double-checking necessary here? \startxmlsetups xml:test \xmlsetsetup{#1}{*}{-} \xmlsetsetup{#1}{doc|element}{xml:*} \stopxmlsetups \xmlregisterdocumentsetup{test}{xml:test} \startxmlsetups xml:doc \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:element 1 \xmldoifelse{#1}{.[@class and contains(@class,'abc')]}{Yes}{No} \par 2 \xmldoifelse{#1}{.[@class and find(@class,"abc")]}{Yes}{No} \par 3 \xmldoifelse{#1}{.[True and contains(@class,'abc')]}{Yes}{No} \par % runs, but no match 4 \xmldoifelse{#1}{.[True and find(@class,"abc")]}{Yes}{No} \par % run, but no match % 5 \xmldoifelse{#1}{.[contains(@class,'abc')]}{Yes}{No} \par % causes an error % 6 \xmldoifelse{#1}{.[find(@class,"abc")]}{Yes}{No} \par % causes an error \stopxmlsetups \startxmlsetups xml:whatever Yes \stopxmlsetups \startbuffer[test] <?xml version="1.0" encoding="UTF-8"?> <doc> <element class="abcdefg">Yes</element> <element>No</element> </doc> \stopbuffer \starttext \xmlprocessbuffer{test}{test}{} \stoptext
On 8/25/2023 8:22 AM, denis.maier@unibe.ch wrote:
Thanks for your help. I can use that, but why is this sort of double-checking necessary here?
because containsa in a function that has checking while find is just the lua find .. so i've added a check in that one too now but no upload Hans ----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
-----Ursprüngliche Nachricht----- Von: Hans Hagen
Gesendet: Freitag, 25. August 2023 09:06 An: Maier, Denis Christian (UB) ; mailing list for ConTeXt users Betreff: Re: [NTG-context] Re: xmldoif: checking for contents of attribute On 8/25/2023 8:22 AM, denis.maier@unibe.ch wrote:
Thanks for your help. I can use that, but why is this sort of double-checking necessary here?
because containsa in a function that has checking while find is just the lua find .. so i've added a check in that one too now but no upload
So, that would mean that the double checking is only necessary for find, not for contains. And indeed the example below works. Is that correct or are there any unwanted side-effects? \startxmlsetups xml:test \xmlsetsetup{#1}{*}{-} \xmlsetsetup{#1}{doc|element}{xml:*} \stopxmlsetups \xmlregisterdocumentsetup{test}{xml:test} \startxmlsetups xml:doc \xmlflush{#1} \stopxmlsetups \startxmlsetups xml:element 1 \xmldoifelse{#1}{.[contains(@class,'abc')]}{Yes}{No} \par 2 \xmldoifelse{#1}{.[@class and find(@class,'abc')]}{Yes}{No} \par % runs, but no match \stopxmlsetups \startxmlsetups xml:whatever Yes \stopxmlsetups \startbuffer[test] <?xml version="1.0" encoding="UTF-8"?> <doc> <element class="abcdefg">Yes</element> <element>No</element> </doc> \stopbuffer \starttext \xmlprocessbuffer{test}{test}{} \stoptext
On 8/25/2023 1:45 PM, denis.maier@unibe.ch wrote:
-----Ursprüngliche Nachricht----- Von: Hans Hagen
Gesendet: Freitag, 25. August 2023 09:06 An: Maier, Denis Christian (UB) ; mailing list for ConTeXt users Betreff: Re: [NTG-context] Re: xmldoif: checking for contents of attribute On 8/25/2023 8:22 AM, denis.maier@unibe.ch wrote:
Thanks for your help. I can use that, but why is this sort of double-checking necessary here?
because containsa in a function that has checking while find is just the lua find .. so i've added a check in that one too now but no upload
So, that would mean that the double checking is only necessary for find, not for contains. And indeed the example below works. Is that correct or are there any unwanted side-effects? correct and in the next upload i'll also catch the find
----------------------------------------------------------------- Hans Hagen | PRAGMA ADE Ridderstraat 27 | 8061 GH Hasselt | The Netherlands tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl -----------------------------------------------------------------
participants (3)
-
denis.maier@unibe.ch
-
Hans Hagen
-
Thomas A. Schmitz