如何在 <xsl:param> 上强制错误没有“选择"属性? [英] How to force errors on a <xsl:param> without "select" attribute?

查看:26
本文介绍了如何在 <xsl:param> 上强制错误没有“选择"属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用 而不指定值,转换器会假定该值是一个空字符串.

If I use <xsl:param> without specifying a value, the transformer assumes that the value is an empty string.

换句话说,如果我忘记指定一个值(例如 <xsl:param name="N"/>),编译器不会发出错误信号.这可能会导致我的程序无声无息地失败,这是一件坏事.

In other words, if I forgot to specify a value (e.g. <xsl:param name="N"/>), the compiler doesn't signal an error. This may cause my program to fail silently, which is a bad thing.

如何指定我的 必须具有显式值?例如,这段代码应该给我一个错误,因为没有指定明确的值:

How can I specify that my <xsl:param> must have an explicit value? For example, this code should give me an error because there is no explicit value specified:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:call-template name="F1"></xsl:call-template>
        <html>
            <head>
                <title></title>
            </head>
            <body>stuff</body>
        </html>
    </xsl:template>
    <xsl:template name="F1">
        <xsl:param name="N"/> <!-- I Should Get An Error Here! -->
    </xsl:template>
</xsl:stylesheet>

我正在寻找 XSLT 1.0 和 XSLT 2.0 的解决方案.

推荐答案

您实际上可以使用一些元 XSLT 来做到这一点:

You could actually do this with a bit of meta-XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />

  <xsl:template match="xsl:call-template">
    <xsl:variable name="template" select="/xsl:stylesheet/xsl:template[@name=current()/@name]"/>
    <xsl:variable name="call" select="." />
    <xsl:variable name="desc">
      <xsl:value-of select="concat('call to named template &quot;',$template/@name,'&quot; in ')"/>
      <xsl:choose>
        <xsl:when test="ancestor::xsl:template/@name">
          <xsl:value-of select="concat('named template &quot;',ancestor::xsl:template/@name,'&quot;')" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat('template matching &quot;',ancestor::xsl:template/@match,'&quot;')" />
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>&#10;</xsl:text>
    </xsl:variable>

    <xsl:for-each select="$template/xsl:param[not(@select)]">
      <xsl:if test="not($call/xsl:with-param[@name=current()/@name])">
        <xsl:value-of select="concat('Missing parameter &quot;',@name,'&quot; in ',$desc)" />
      </xsl:if>
    </xsl:for-each>

    <xsl:for-each select="xsl:with-param">
      <xsl:if test="not($template/xsl:with-param[@name=current()/@name])">
        <xsl:value-of select="concat('Unrecognised parameter &quot;',@name,'&quot; in ',$desc)" />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="text()" />
</xsl:stylesheet>

此样式表将任何样式表作为输入,并检查所有调用模板的参数是否正确,如果有任何错误,则输出消息.

This stylesheet takes any stylesheet as an input, and checks that all call-template's have the right parameters, outputting a message if there's any errors.

这显然不会将错误检查放在转换器本身中,但它会一次性列出所有错误,并且还可以扩展以检查其他问题.

This obviously isn't going to put the error checking in the transformer itself, but it will list ALL errors in one go, and can potentially be extended to check for other issues as well.

我已经对其进行了调整以处理可选参数,并添加了一种描述错误位置的方法;这实际上是一个重新设计,可选参数只是简单地计算它们会很棘手,所以我删除了这一点.无论如何,每个错误都被逐项列出,因此计数并不是真正必要的.

I've adapted it to handle optional parameters, and added in a means of describing where the error is; it's actually a bit of a redesign, with optional parameters simply counting them was going to be tricky, so I removed that bit. Every error is itemized anyway, so the count wasn't really necessary.

这篇关于如何在 &lt;xsl:param&gt; 上强制错误没有“选择"属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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