Re: [xsl] how to make a log file in XSLT?

Subject: Re: [xsl] how to make a log file in XSLT?
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 12 Mar 2023 21:42:16 -0000
The requirement that the "log file" must be an XML document is
too-restricting in this problem.

This is why people generally don't write their log records as elements of
an XML file.

There are logging systems that write the log records in a database table
(thus the log records from a farm of web servers is collected just in one
place instead of N and also writing a log record within a transaction
solves the concurrency problem).

And having as a field the log-timestamp frees us from any ordering
requirement, too. Furthermore such records can be grouped on the values of
other fields, such as LogLevel, EventId, ApplicationName, Category, ...,
etc.

Taking all this into account, if I needed logging from an XSLT
transformation, I would just send requests to a web-service, and let it do
the writing.

Even this could be a little-bit tricky, because the doc() and document()
function are required (I think) to use the GET Http Verb, and this could
have unexpected results if caching were enabled. To explicitly disable
caching for GET, one must be able to create a specific header, something
that is again not (yet) possible in pure XSLT/XPath. Ideally we should be
able to use the POST or PUT verb for such service-endpoints.

I also understand that one may want to get the log represented as an XML
document, but again, let us leave this serialization to the respective
service endpoint -- it should be none of our business how the data is
actually organized behind the curtains.

Thanks,
Dimitre

On Sun, Mar 12, 2023 at 1:11b/PM Jean-Luc Chevillard
jeanluc.chevillard@xxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
wrote:

> Dear list members,
>
> I have been wondering for a long time whether there was a method for
> automatically recording some elements of information every time I run
> a given script.
>
> As a minimum example, I would start with an empty file
> +++++++++++++++
> <?xml version="1.0" encoding="UTF-8"?>
> <log>
> </log>
> +++++++++++++++++++++++
>
> After running the script once, the file would become
> +++++++++++++++
> <?xml version="1.0" encoding="UTF-8"?>
> <log>
>    <entry>[2023-03-12 (19:53)]</entry>
> </log>
> +++++++++++++++++++++++
>
> After running the script a second time, one hour later, the file would
> become
> +++++++++++++++
> <?xml version="1.0" encoding="UTF-8"?>
> <log>
>    <entry>[2023-03-12 (20:53)]</entry>
>   <entry>[2023-03-12 (19:53)]</entry>
> </log>
> +++++++++++++++++++++++
>
> I have just tried doing this by applying the script at the bottom of
> this message (in which "copy.xslt" is the identity transform) to a
> base file, but as I expected, I obtained an error message stating:
>
> System ID: C:\JLC\XML_tests\cEnAvaraiyam\log_book_experiment.xslt
> Scenario: 4a_log_experiment
> XML file: C:\JLC\XML_tests\cEnAvaraiyam\cEnA_MSS_rectangles_chart.xml
> XSL file: C:\JLC\XML_tests\cEnAvaraiyam\log_book_experiment.xslt
> Engine name: Saxon-PE 11.4
> Severity: fatal
> Problem ID: XTRE1500
> Description: Cannot read a document that was written during the same
> transformation:
> file:/C:/JLC/XML_tests/cEnAvaraiyam/experimental_log_book.xml
> Start location: 17:63
> Length: 1
> URL: http://www.w3.org/TR/xslt20/#err-XTRE1500
>
> Is there a method for obtaining the desired result by using XSLT?
>
> Thanks for your answers
>
> -- Jean-Luc
>
> ++++++++++++++++++++++++++++
> log_book_experiment.xslt
> ++++++++++++++++++++++++++++
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>     xmlns:xs="http://www.w3.org/2001/XMLSchema";
>     exclude-result-prefixes="xs"
>     version="2.0">
>
>     <xsl:import href="copy.xslt"/>
>
>     <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
>
>     <xsl:variable name="TIME_STAMP"
> select="concat('[',format-date(current-date(), '[Y]-[M02]-[D02]'), '
> (', format-time(current-time(), '[H]:[m]'), ')]')"/>
>
>     <xsl:variable name="Log_file_name"
> select="'experimental_log_book.xml'"/>
>
>     <xsl:variable name="current_log_content"
> select="document($Log_file_name)"/>
>
>     <xsl:variable name="new_log_content">
>         <log>
>             <entry><xsl:value-of select="$TIME_STAMP"/></entry>
>             <xsl:for-each select="$current_log_content//entry">
>                 <xsl:copy-of select="."/>
>             </xsl:for-each>
>         </log>
>     </xsl:variable>
>
>     <xsl:template match="/">
>         <xsl:result-document href="{$Log_file_name}">
>             <xsl:copy-of select="$new_log_content"></xsl:copy-of>
>         </xsl:result-document>
>         <xsl:apply-templates select="root"/>
>     </xsl:template>
>
>
> </xsl:stylesheet>
> +++++++++++++++++++++++++++++++++

Current Thread