XSLT 参数不起作用

发布于 2024-12-14 09:34:00 字数 1026 浏览 2 评论 0原文

我在 Java 中使用 XSLT 转换器。我确信问题出在我输入 XSLT 语法的方式上。

基本上,我必须查看 XML 节点之一中的属性“id”是否相当于一个值,XSL 应该处理一个模板。我需要将此测试值作为 xsl:param 传递。当我在静态值上测试它时,它工作得很酷:

<ul class="levelOneNav">
 <xsl:for-each select="level1[@id='sub-page-1']">
  <xsl:call-template name="level1"/>
  </xsl:for-each>
 <div class="clear"></div>
</ul>       

测试部分位于上面的第二行。 AFAIK 在字符串中放入参数,格式为 其中 $ContentID 是声明为 的 xsl:param

所以这是我正在键入的代码,

<ul class="levelOneNav">
 <xsl:for-each select="level1[@id='$ContentID']">
  <xsl:call-template name="level1"/>
  </xsl:for-each>
 <div class="clear"></div>
</ul>       

但它不起作用。我还尝试过使用 它也失败了。

任何建议将不胜感激。如果您觉得我应该学习一些东西,请给我链接。感谢您抽出时间...

I am using a XSLT Transformer in Java. I am sure the problem is in the way I'm typing in the XSLT syntax.

Basically, I have to see if an attribute 'id' in one of the nodes of XML is equivalent to a value, the XSL should process a template. I need to pass this test value as a xsl:param. When I test it on a static value, it works cool:

<ul class="levelOneNav">
 <xsl:for-each select="level1[@id='sub-page-1']">
  <xsl:call-template name="level1"/>
  </xsl:for-each>
 <div class="clear"></div>
</ul>       

The testing portion is in second line above. AFAIK to put in a param inside the strings, the format is <xsl:for-each select="level1[@id='$ContentID']"> in which $ContentID is the xsl:param declared as <xsl:param name="ContentID"/>

So this is the code I'm keying in

<ul class="levelOneNav">
 <xsl:for-each select="level1[@id='$ContentID']">
  <xsl:call-template name="level1"/>
  </xsl:for-each>
 <div class="clear"></div>
</ul>       

but it doesn't work. I've also tried using <xsl:for-each select="level1[@id='{$ContentID}']">
and it also fails.

Any advise is appreciated greatly. Please give me links if you feel I should learn something. Thanks for your time ...

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

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

发布评论

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

评论(2

诠释孤独 2024-12-21 09:34:00

不,“据我所知”你错了。 XSLT 属性中的某些字符串值当然接受 "{$param}" 语法(注意大括号),但在您的情况下,您所追求的要简单得多:< code>@id=$ContentID

编辑:请注意,“{$param}”适用于 XSLT 属性,请参阅示例:

<xsl:element name="{$namespace}:{$elementName}">
   <xsl:attribute name="{$attrName}"><xsl:value-of select="$value"/></xsl:attribute>
</xsl:element>

No, that “AFAIK” is where you are wrong. Some string values in XSLT attributes certainly do accept a "{$param}" syntax (note the braces), but in your case what you are after is much simpler: @id=$ContentID

EDIT: Note that the '{$param}' applies for XSLT attributes, see example:

<xsl:element name="{$namespace}:{$elementName}">
   <xsl:attribute name="{$attrName}"><xsl:value-of select="$value"/></xsl:attribute>
</xsl:element>
把梦留给海 2024-12-21 09:34:00

尝试

<ul class="levelOneNav">
<xsl:variable name="contentID" select="'sub-page-1'" />
 <xsl:for-each select="level1[@id=$ContentID]">
  <xsl:call-template name="level1"/>
  </xsl:for-each>
 <div class="clear"></div>
</ul>

变量或参数将以同样的方式工作。
请注意,您必须删除 $ContentID 周围的简单引号。
另外,如果 level1/@id 是唯一的,则不需要 foreach。

下面是一个将变量作为参数发送的示例。

<ul class="levelOneNav">
    <xsl:variable name="contentID" select="'sub-page-1'" />
     <xsl:for-each select="level1[@id=$ContentID]">
      <xsl:call-template name="level1" >
           <xsl:with-param name="foo" select="$ContenID" />
      </xsl:call-template>
      </xsl:for-each>
     <div class="clear"></div>
</ul>

try

<ul class="levelOneNav">
<xsl:variable name="contentID" select="'sub-page-1'" />
 <xsl:for-each select="level1[@id=$ContentID]">
  <xsl:call-template name="level1"/>
  </xsl:for-each>
 <div class="clear"></div>
</ul>

variable or param will work same way.
becareful you must remove simple quote around $ContentID.
another things if level1/@id is unique you don't need the foreach.

below an exemple to send the variable as param.

<ul class="levelOneNav">
    <xsl:variable name="contentID" select="'sub-page-1'" />
     <xsl:for-each select="level1[@id=$ContentID]">
      <xsl:call-template name="level1" >
           <xsl:with-param name="foo" select="$ContenID" />
      </xsl:call-template>
      </xsl:for-each>
     <div class="clear"></div>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文