使用XSLT记录 [英] Logging with XSLT

查看:66
本文介绍了使用XSLT记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XSLT转换期间,如何以及在何处出于调试和性能目的输出日志消息?

How and where could I output log messages for debug and performance purposes during an XSLT transformation?

我猜最简单的方法是使用这样的表达式:

I guess the simplest method is using expressions like this:

<xsl:text>message text</xsl:text>

在代码中的任何位置,如有需要,请使用xsl:value-of.

here and there in the code, using xsl:value-of if needed.

但是此方法将消息打印在输出文件(在我的情况下为HTML页面)的很多位置,而不是始终在同一位置(如日志文件)打印(在我的情况下为HTML页面).

But this method prints the messages in a lot of places in the output file (HTML page in my case), that is where it is called, and not always in the same place (like a log file).

这是唯一的方法还是是否有更好的解决方案?谢谢!

Is this the only way or is there a better solution? Thanks!

推荐答案

这正是<xsl:message>的目标.但是,输出位置完全取决于处理器.我只有一台Mac,但可悲的是,Firefox和Safari都抑制了<xsl:message>输出.我希望MSIE也会这样做.

This is exactly what <xsl:message> is designed for. However, the output location is entirely dependent on the processor. I only have a Mac handy but, sadly, both Firefox and Safari suppress the <xsl:message> output. I expect MSIE will do the same.

鉴于此,我认为您最好的选择是使用<xsl:comment>生成日志.像下面这样的东西应该可以解决问题:

Given that, I think your best bet is to use <xsl:comment> to generate your logs. Something like the below should do the trick:

<xsl:template match='my-element'>
   <xsl:comment>Entering my-element template</xsl:comment>
   <p class='my-element'><xsl:apply-templates/></p>
   <xsl:comment>Leaving my-element template</xsl:comment>
</xsl:template>

这将在输出中为您提供类似的信息:

That would give you something like this in the output:

<!-- Entering my-element template -->
<p class='my-element'>...</p>
<!-- Leaving my-element template -->

很显然,您可以将所需的任何日志记录放入该输出中.我会考虑创建类似以下内容的东西,并使用它来运行日志记录.这引用了一个称为"enable-logging"的全局参数,以确定是否应该进行日志记录.

Clearly, you can put whatever logging you want into that that output. I would consider creating something like the following and using it to run your logging. This references a global param called 'enable-logging' to determine if logging should occur or not.

<xsl:template name='create-log'>
   <xsl:param name='message'/>
   <xsl:if test="$enable-logging = 'yes'">
       <xsl:comment><xsl:value-of select='$message'/></xsl:comment/>
   </xsl:if>
</xsl:template>

在样式表中将其用作:

<xsl:template match='my-element'>
   <xsl:call-template name='create-log'>
     <xsl:with-param name='message'/>Entering my-element template</xsl:with-param>
   </xsl:call-template>
   <p class='my-element'><xsl:apply-templates/></p>
   <xsl:call-template name='create-log'>
     <xsl:with-param name='message'/>Leaving my-element template</xsl:with-param>
   </xsl:call-template>
</xsl:template>

以这种方式进行操作的一个好处是,可以在更完整的环境中将<xsl:comment>更改为<xsl:message>.它更冗长,但更笼统.

One benefit of doing it this way is you can change that <xsl:comment> to <xsl:message> when in a more complete environment. It is more verbose but more general.

这篇关于使用XSLT记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆