使用EXSLT对变量执行XPath 1.0 max函数

使用EXSLT对变量执行XPath 1.0 max函数,xslt,xpath,exslt,Xslt,Xpath,Exslt,我正在编写一个XPath函数,其工作原理与XPath2.0 fn:max函数类似。返回多个参数的最大值的函数 在搜索了很多之后,我发现: <?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="htt

我正在编写一个XPath函数,其工作原理与XPath2.0 fn:max函数类似。返回多个参数的最大值的函数

在搜索了很多之后,我发现:

<?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函数可以接受更多元素

干杯

Jan

您是否可以指定xml(用于节点集)作为输入参数

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


...
13
123
18

您是否可以指定xml(用于节点集)作为输入参数

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


...
13
123
18

我面前没有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>

1.
2.
3.


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

我面前没有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>

1.
2.
3.


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

谢谢您的想法

你帮助我更好地理解了一切。但我的初衷是得到一份工作 方便的XPath函数,可用于xsl变量

<!-- 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>

$maximageheight
对于固定数量的参数,可以实现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>


但我看不到一种方法可以通过可变数量的参数来实现它。

谢谢您的想法

你帮助我更好地理解了一切。但我的初衷是得到一份工作 方便的XPath函数,可用于xsl变量

<!-- 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>

$maximageheight
对于固定数量的参数,可以实现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>


但是我看不到一种方法来实现它,因为参数的数量是可变的。

msxsl:node-set纯粹是因为这是我必须使用的IDE;正如我所提到的,exslt:node set也应该这样做。我无法使用exslt进行测试,我宁愿发布一个经过测试的答案;正如我所提到的,exslt:node set也应该这样做。我只是不能用exslt测试,我更愿意发布一个测试过的答案。那么也许它不能做到。。。顺便说一句,这将是一个更好的更新的问题,而不是一个答案。那么它可能无法做到。。。顺便说一句,这将更好地作为问题的更新,而不是答案。