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

发布于 2024-07-22 15:52:14 字数 1189 浏览 3 评论 0原文

我正在搜索一个类似于 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 函数可以接受更多元素?

干杯

I am serching an XPath function that works like the XPath 2.0 fn:max function. A function that returns the maximum of several parameters.

After searching a lot I figured out this way:

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

Is it possible to do it so that my max function can take more elements?

Cheers
Jan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停 2024-07-29 15:52:15

想必您可以指定 xml(用于节点集)作为输入参数?

我不是在 exslt 上“up”,而是使用 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>

Presumably you could specify the xml (for the node-set) as the input argument?

I'm not "up" on exslt, but using msxsl (just for the node-set function, which is also in 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>
绿光 2024-07-29 15:52:15

谢谢你的想法。

你帮助我更好地理解了一切。 但我的初衷是想得到一个
方便的 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>

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

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

Thank you for your ideas.

You helped me to understand everything a bit better. But my original intention was to get a
handy XPath function that works on xsl variables.

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

For a fixed umber of parameters it would be possible to implement an exslt function:

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

But i do not see a way to implement it vor a variable number of parameters.

只怪假的太真实 2024-07-29 15:52:14

我面前没有 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 数据,您可以使用 xsl:with-param 上的 select= 属性自动选择您想要的节点比较。

I don't have my XSLT 1.0 book in front of me, but I think the key here is that you can select 'node sets' and set those equal to your parameter variable, rather than having one-node-per-parameter.
Here's a rough guess:

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

edit: re-read the question along with some XSLT 1.0 guidance. It should resemble the other answer, simplified only slightly. Keep in mind that if the numbers you want come from the XML data, you can use the select= attribute on xsl:with-param to automatically select the nodes you want to compare.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文