将参数从jsp传递到xsl

时间:2022-02-27 08:10:20

My code below seems logical however i don't know why the sorting doesn't work with the error "Variable or parameter 'sort' is undefined.'"? Im suspecting there are something wrong with declaring param in xsl. Could anyone point my mistake? thanks

我的下面的代码似乎合乎逻辑但是我不知道为什么排序不能用于错误“变量或参数'排序'未定义。'”?我怀疑在xsl中声明param有什么问题。谁能指出我的错误?谢谢

java code to pass parameter

java代码传递参数

String sort = "rating";
transformer.setParameter("sort", sort); /It will control the sort in xsl

xml file

xml文件

    <?xml version="1.0" ?>

<cd>
  <title>A Funk Odyssey</title>
  <artist>Jamiroquai</artist>

  <tracklist>
    <track id="1">
      <title>Feels So Good</title>
      <time>4:38</time>
      <rating>2</rating>
    </track>

    <track id="2">
      <title>Little L</title>
      <time>4:10</time>
      <rating>5</rating>
    </track>

    <track id="3">
      <title>You Give Me Something</title>
      <time>5:02</time>
      <rating>3</rating>
    </track>

    <track id="4">
      <title>Corner of the Earth</title>
      <time>3:57</time>
      <rating>1</rating>
    </track>
  </tracklist>


</cd>

This is my xsl

这是我的xsl

            <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
    <xsl:param name="sort" select="title"/>
    <xsl:template match="/">
        <table border="1">
            <thead>
                <tr>
                    <th><a href="#">Title</a></th>
                    <th><a href="#">Time</a></th>
                    <th><a href="#">Rating</a></th>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="cd/tracklist/track">
                    <xsl:sort select="$sort"/>
                    <tr>
                        <td><xsl:value-of select="title" /></td>
                        <td><xsl:value-of select="time" /></td>
                        <td><xsl:value-of select="rating" /></td>
                    </tr>
                </xsl:for-each>
            </tbody>
        </table>
    </xsl:template>
</xsl:stylesheet>

1 个解决方案

#1


4  

In your xsl:param declaration you are trying to default to $sort which is not defined at the time xsl:param is evaluated. It does look like a reference to itself.

在xsl:param声明中,您尝试默认为$ sort,而xsl:param在评估时未定义。它看起来像是对自己的引用。

If you don't need a default then just change your parameter declaration to:

如果您不需要默认值,则只需将参数声明更改为:

<xsl:param name="sort"/>

or default to a string value:

或默认为字符串值:

<xsl:param name="sort" select="'title'"/>

or

要么

<xsl:param name="sort">title</xsl:param>

That said, we have only addressed the parameter declaration issue. Now on to sorting. The xsl:sort needs an expression, it won't convert a string value into XPath like you expect it to.

也就是说,我们只解决了参数声明问题。现在进行排序。 xsl:sort需要一个表达式,它不会像你期望的那样将字符串值转换为XPath。

Here's a solution: Using Variables in <xsl:sort select=""/>.

这是一个解决方案:在 中使用变量。 :sort>

You would basically do something like:

你基本上会做类似的事情:

<xsl:sort select="*[name() = $sort]"/>

#1


4  

In your xsl:param declaration you are trying to default to $sort which is not defined at the time xsl:param is evaluated. It does look like a reference to itself.

在xsl:param声明中,您尝试默认为$ sort,而xsl:param在评估时未定义。它看起来像是对自己的引用。

If you don't need a default then just change your parameter declaration to:

如果您不需要默认值,则只需将参数声明更改为:

<xsl:param name="sort"/>

or default to a string value:

或默认为字符串值:

<xsl:param name="sort" select="'title'"/>

or

要么

<xsl:param name="sort">title</xsl:param>

That said, we have only addressed the parameter declaration issue. Now on to sorting. The xsl:sort needs an expression, it won't convert a string value into XPath like you expect it to.

也就是说,我们只解决了参数声明问题。现在进行排序。 xsl:sort需要一个表达式,它不会像你期望的那样将字符串值转换为XPath。

Here's a solution: Using Variables in <xsl:sort select=""/>.

这是一个解决方案:在 中使用变量。 :sort>

You would basically do something like:

你基本上会做类似的事情:

<xsl:sort select="*[name() = $sort]"/>