Re: [xsl] How do you avoid relearning the same coding technique over and over?

Subject: Re: [xsl] How do you avoid relearning the same coding technique over and over?
From: "Wendell Piez wapiez@xxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 6 Feb 2025 17:02:08 -0000
Hello,

It is interesting to note the code Dimitre posted does not recurse into
subfolders.

Which might be fine, indeed his specification does not say to collect
directories and process them the same way. For the AI to do that might be
unexpected, and it might be wrong.

But 'might be' is the operative term there. Whether that is a problem, or
is easily correctable, misses the point.

The point is that when Roger writes that folder-reading code again for the
umpteenth time, he is not actually writing the same code again. He thinks
he is being inefficient because he remembers writing it before. But he
wouldn't think that if he didn't remember it. So, what David C says - he
just has to write everything perfectly the first time and also remember
everything, problem solved. Or to write everything only adequately, and
forget everything.

Alternatively, he could accept the uncomfortable in-between, but look at
every occasion of rewriting to provide an opportunity to make it better,
including easier to find later. Instead of blaming himself, he could see it
as an engineering problem -- too much latency between problem statement and
solution, whenever he 'rewrites' instead of 'finds'.

This approach recognizes that "it is fine if the folder reader does not
recurse into subfolders" is a statement that is true only in context, and
does not presume that a general solution is needed (and needs to be
findable) for every particular problem. Maybe he can reduce that latency by
finding more and rewriting less. But he might find that being able to find
old code doesn't give the efficiency gains he expected -- because only
sometimes do the folders have to recurse (etc.)

The actual problem is the assumption that either "finding" or "rewriting"
is a problem, because he will actually always be doing both.

If and when the AI bot becomes trustworthy enough (note I do not say
'competent') to use, it will not help with this, which is on a higher level
than being about to find, or serve up, boilerplate code. Look up "technical
debt" for more. It's simply a matter of which work you prioritize, and
when, and how that bears on work later -- something knowable only
probabilistically if at all.

I would also like to see AI-generated code solving this problem to a prompt
written by someone not as knowledgeable as Dimitre and without his guidance.

Plus, I would also like to see it accompanied by runnable XSpec. A test
suite is good not just for testing, but also for demonstration and
documentation -- and I dare say, for findability. (This might be a bit
tricky for the EXPath file module.)

Cheers, Wendell





On Thu, Feb 6, 2025 at 11:12b/AM Dimitre Novatchev dnovatchev@xxxxxxxxx <
xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

> Show example code of an XSLT transformation that uses the EXpath file
> module to iterate over a set of folders and then for each folder iterate
> over its files.
> ChatGPT said:
>
> Sure! Below is an example of an XSLT transformation that utilizes the
*EXPath
> File Module* to iterate over a set of folders and then, for each folder,
> iterate over its files. This assumes that you are using an XSLT 2.0 or 3.0
> processor that supports EXPath extensions, such as *Saxon with EXPath
> extensions*.
> Example XSLT Code:
>
> xml
> CopyEdit
> <xsl:stylesheet version="3.0"
>     xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>     xmlns:file="http://expath.org/ns/file";
>     exclude-result-prefixes="file">
>
>     <!-- Define the base directory -->
>     <xsl:param name="base-dir" select="'/path/to/base/directory'"
as="xs:string"/>
>
>     <xsl:template match="/">
>         <folders>
>             <!-- Iterate over the directories inside the base directory -->
>             <xsl:for-each select="file:list($base-dir)">
>                 <xsl:variable name="folder-path"
select="file:resolve-path(concat($base-dir, '/', .))"/>
>
>                 <!-- Check if it's a directory -->
>                 <xsl:if test="file:is-dir($folder-path)">
>                     <folder name="{.}">
>                         <!-- Iterate over the files in this folder -->
>                         <xsl:for-each select="file:list($folder-path)">
>                             <xsl:variable name="file-path"
select="file:resolve-path(concat($folder-path, '/', .))"/>
>
>                             <!-- Check if it's a file -->
>                             <xsl:if test="file:is-file($file-path)">
>                                 <file name="{.}"/>
>                             </xsl:if>
>                         </xsl:for-each>
>                     </folder>
>                 </xsl:if>
>             </xsl:for-each>
>         </folders>
>     </xsl:template>
> </xsl:stylesheet>
>
> Explanation:
>
>    1. *file:list($base-dir)* b Retrieves the list of items in the base
>    directory.
>    2. *file:is-dir($folder-path)* b Checks if the current item is a
>    directory.
>    3. *file:list($folder-path)* b Lists the contents of each directory.
>    4. *file:is-file($file-path)* b Checks if the current item is a file.
>    5. The output XML structure includes <folders>, with each <folder>
>    containing its <file> elements.
>
> Sample Output:
>
> xml
> CopyEdit
> <folders>
>     <folder name="Documents">
>         <file name="report.pdf"/>
>         <file name="notes.txt"/>
>     </folder>
>     <folder name="Images">
>         <file name="photo.jpg"/>
>         <file name="logo.png"/>
>     </folder></folders>
>
> This approach efficiently traverses directories and files using *EXPath
> File Module* functions. Let me know if you need any modifications! p
> 4o
>
> On Thu, Feb 6, 2025 at 4:19b/AM Roger L Costello costello@xxxxxxxxx <
> xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
>> Hi Folks,
>>
>> A few days ago, I needed some code to iterate over a set of folders and
>> then for each folder iterate over its files.
>>
>> Over the years I have solved that problem--use the expath file
>> module--many times. In my most recent need, I spent a few minutes
searching
>> my file system to find an XSLT program that uses the expath module, but I
>> didn't find anything, so I spent time relearning how to use the expath
file
>> module. What a waste of time.
>>
>> In the ideal world, when I develop some code--such as code to iterate
>> over folders and subfolders--I would pause what I'm doing, create example
>> code showing how to solve the task, and store that example code in some
>> location on my file system that I'll remember 6 months, 6 years later.
>> Alas, I'm in a hurry. I don't do that.
>>
>> What's the solution? Discipline? No matter how much I'm in a hurry, stop
>> and create an example. Is that the solution?
>>
>> How do you avoid relearning the same coding technique over and over?
>>
>> /Roger
>>
>>
>>
>
> --
> Cheers,
> Dimitre Novatchev
> ---------------------------------------
> Truly great madness cannot be achieved without significant intelligence.
> ---------------------------------------
> To invent, you need a good imagination and a pile of junk
> -------------------------------------
> Never fight an inanimate object
> -------------------------------------
> To avoid situations in which you might make mistakes may be the
> biggest mistake of all
> ------------------------------------
> Quality means doing it right when no one is looking.
> -------------------------------------
> You've achieved success in your field when you don't know whether what
> you're doing is work or play
> -------------------------------------
> To achieve the impossible dream, try going to sleep.
> -------------------------------------
> Facts do not cease to exist because they are ignored.
> -------------------------------------
> Typing monkeys will write all Shakespeare's works in 200yrs.Will they
> write all patents, too? :)
> -------------------------------------
> Sanity is madness put to good use.
> -------------------------------------
> I finally figured out the only reason to be alive is to enjoy it.
>
> XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
> EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/174322> (by
> email <>)
>


--
...Wendell Piez... ...wendell -at- nist -dot- gov...
...wendellpiez.com... ...pellucidliterature.org... ...pausepress.org...
...github.com/wendellpiez... ...gitlab.coko.foundation/wendell...

Current Thread