【问题标题】:XSLT: Foreach iterates for each item, but displays the value of the first item?XSLT:Foreach 迭代每个项目,但显示第一个项目的值?
【发布时间】:2009-05-09 21:20:30
【问题描述】:

我有一个项目列表,我想为每个项目设置一个 url。

列表:

 <root>
   <tags>
     <tag>open source</tag>
     <tag>open</tag>
     <tag>advertisement</tag>
     <tag>ad</tag>
   </tags>
 </root>

XSLT:

<xsl:template match="*">
    <div class="tags">
      <xsl:for-each select="/post/tags/tag">
          <a href="#">
            <xsl:value-of select="//tag"/>
          </a>
      </xsl:for-each>
    </div>
</xsl:template>

输出:

  <div class="tags"> 
    <a href="#">open source</a> 
    <a href="#">open source</a> 
    <a href="#">open source</a> 
    <a href="#">open source</a> 
  </div> 

我做错了什么?

【问题讨论】:

    标签: xslt foreach


    【解决方案1】:

    一种更正确的 XSLT 方法是添加一个“标签”模板并修改您的原始模板:

    <xsl:template match="*">
        <div class="tags">
            <xsl:apply-templates select="tag" />
        </div>
    </xsl:template>
    
    <xsl:template match="tag">
          <a href="#">
            <xsl:value-of select="."/>
          </a>
    </xsl:template>
    

    【讨论】:

      【解决方案2】:

      您对 value-of 表达式所做的是选择 xml 文档中的所有标记节点:

      <xsl:value-of select="//tag"/>
      

      这样做的效果是只有第一个选定的节点将用于值。

      您可以改用以下内容:

      <xsl:value-of select="."/>
      

      选择=“。”将从 for-each 中选择当前节点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 2021-08-30
        • 2018-01-29
        • 1970-01-01
        • 2013-03-26
        • 2019-08-08
        • 2021-10-02
        相关资源
        最近更新 更多