1

我正在搜索一个与 XPath 2.0 fn:max 函数类似的 XPath 函数。返回多个参数中最大值的函数。

经过大量搜索后,我发现了这种方式:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:math="http://exslt.org/math" 
xmlns:exslt="http://exslt.org/common" 
xmlns:func="http://exslt.org/functions"
xmlns:my="http://myns.com"
extension-element-prefixes="math exslt func">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <root>
            <xsl:value-of select="my:max(1,2)"/>
        </root>
    </xsl:template>


    <func:function name="my:max">
        <xsl:param name="e1"/>
        <xsl:param name="e2"/>

        <xsl:variable name="x">
            <val><xsl:value-of select="$e1"/></val>
            <val><xsl:value-of select="$e2"/></val>
        </xsl:variable>

        <func:result select="math:max(exslt:node-set($x)/val)"/>
    </func:function>
</xsl:stylesheet>

是否可以这样做,以便我的 max 函数可以采用更多元素?

干杯
一月

4

3 回答 3

1

我面前没有我的 XSLT 1.0 书,但我认为这里的关键是您可以选择“节点集”并将其设置为等于您的参数变量,而不是每个参数一个节点。这是一个粗略的猜测:

<xsl:template match="/">
    <root>
      <xsl:call-template name="max">
        <xsl:with-param name="values">
          <val>1</val>
          <val>2</val>
          <val>3</val>
        </xsl:with-param>
      </xsl:call-template>
    </root>
</xsl:template>


<func:function name="my:max">
    <xsl:param name="x"/>

    <func:result select="math:max($x/val/*)"/>
</func:function>

编辑:重新阅读问题以及一些 XSLT 1.0 指南。它应该类似于另一个答案,只是稍微简化了一点。请记住,如果您想要的数字来自 XML 数据,您可以使用select=属性 onxsl:with-param自动选择您想要比较的节点。

于 2009-06-11T19:04:58.587 回答
0

大概您可以将 xml(用于节点集)指定为输入参数?

我不是在 exslt 上“启动”,而是使用 msxsl(仅用于node-set函数,它也在 exslt 中):

  <xsl:template name="max">
    <xsl:param name="values"/>
    <xsl:for-each select="msxsl:node-set($values)/val">
      <xsl:sort data-type="number" order="descending"/>
      <xsl:if test="position()=1">
        <xsl:value-of select="."/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  ...
  <xsl:call-template name="max">
    <xsl:with-param name="values">
      <val>13</val>
      <val>123</val>
      <val>18</val>
    </xsl:with-param>
  </xsl:call-template>
于 2009-06-07T22:43:32.750 回答
0

谢谢你的想法。

你帮助我更好地理解了一切。但我的初衷是获得一个适用于 xsl 变量的方便的 XPath 函数。

<!-- works with XPath 2.0 -->
<xst:template match="img/@height">
  <xsl:variable name="$maximageheight" select="200">
  <xsl:value-of select="fn:max( $maximageheight , . )"/>
</xsl:template>

<!-- until now the only way I see to do the same in XSL 1.0 -->
<xst:template match="img/@height">
<xsl:variable name="$maximageheight" select="200">
  <xsl:call-template name="max">
    <xsl:with-param name="values">
      <val>$maximageheight</val>
      <val><xsl:value-of select="."/></val>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

对于固定数量的参数,可以实现 exslt 函数:

<func:function name="my:max" xmlns:func="http://exslt.org/functions">
  <xsl:param name="e1"/>
  <xsl:param name="e2"/>

  <xsl:variable name="x">
    <val><xsl:value-of select="$e1"/></val>
    <val><xsl:value-of select="$e2"/></val>
  </xsl:variable>

  <func:result select="math:max(exslt:node-set($x)/val)"/>
</func:function>

但我没有看到一种方法来实现它或可变数量的参数。

于 2009-06-12T10:48:58.757 回答