首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按类别过滤物品

按类别过滤物品
EN

Stack Overflow用户
提问于 2014-04-17 01:00:15
回答 1查看 103关注 0票数 0

我试图在我的主页上显示来自特定类别的文章。

xml删除。

注意:我需要从获取的类别是id=38。这是显示,但可能是偶然的,因为它包括在目前的文章。

我已经使用我在这里找到的解决方案修复了这个问题。很简单。我的数据源是将类别过滤为{$ds-类别}。我把它改成了38个,而且效果很好。我就知道这会很简单。

这是我的主页,它输出空白的div来代替应该显示的内容:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="../utilities/master.xsl"/>
<xsl:import href="../utilities/get-article.xsl"/>
<xsl:import href="../utilities/pagination.xsl"/>
<xsl:import href="../utilities/get-notes.xsl"/>
<xsl:import href="../utilities/get-comments.xsl"/>
<xsl:template match="data">
<div id="content" class="container">
        <div id="content">
          <div class="col content">
            <div class="col span1">
              <h4 class="n4">Recent entries</h4>
            </div>
          </div>
    <xsl:apply-templates select="homepage-articles/entry" mode="short"/>
    <xsl:apply-templates select="homepage-updates/entry" mode="updates"/>
                  </div>
        <div id="sidebar" class="col last">
            <xsl:call-template name="contact"/>
              <xsl:call-template name="social"/>
            <xsl:call-template name="category"/>
            <xsl:call-template name="tag"/>
            <xsl:call-template name="external-links"/>
        </div>
            </div>
</xsl:template>
</xsl:stylesheet>

然后我的get模板看起来如下所示:

代码语言:javascript
复制
  <xsl:template match="entry" mode="updates">
<div>
  <xsl:for-each select="entry[@handle = updates]">
            <div class="post-meta">
              <span><xsl:call-template name="format-date">
                    <xsl:with-param name="date" select="date"/>
                    <xsl:with-param name="format" select="'D M'"/>
                </xsl:call-template></span>
              <span id="year"><xsl:call-template name="format-date">
                <xsl:with-param name="date" select="date"/>
                    <xsl:with-param name="format" select="'Y'"/>
              </xsl:call-template></span>
                <ul>
                    <li>
                        <xsl:text>Tagged as: </xsl:text>
                        <xsl:for-each select="tags/item">
                            <a href="{$root}/archive/tag/{@handle}"><xsl:value-of select="."/></a>
                            <xsl:if test="position() != last()">, </xsl:if>
                        </xsl:for-each>
                    </li>
                    <li><a href="{$root}/post/{title/@handle}#comments"><xsl:value-of select="@comments"/> comment<xsl:if test="@comments != 1">s</xsl:if></a></li>
                </ul>
            </div>
            <div class="post-content last span-7">
                <h3><a href="{$root}/post/{title/@handle}"><xsl:value-of select="title"/></a></h3>
                <xsl:apply-templates select="body/*[position() &lt; 3]" mode="html"/>
                        <xsl:call-template name="get-images" />
              <p style="float: right; clear: both;"><a href="{$root}/post/{title/@handle}" title="Read {title}">Read the full article</a></p>
            </div>
    </xsl:for-each>
        </div>
    </xsl:template>

谢谢你的帮助!一旦我解决了这个问题,它肯定能帮助我避免遇到更多的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-17 07:53:49

在第一个模板中,有一个xsl:apply-templates

代码语言:javascript
复制
<xsl:apply-templates select="homepage-updates/entry" mode="updates"/>

您还拥有与此匹配的模板。

代码语言:javascript
复制
<xsl:template match="entry" mode="updates">

到现在为止还好。但是在这个“条目”模板中,您可以执行以下操作

代码语言:javascript
复制
<xsl:for-each select="entry[@handle = updates]">

这是在寻找一个条目元素,它是您正在匹配的当前条目元素的子元素,或者在您的XML中没有。

尝试更改模板匹配以匹配父元素:

代码语言:javascript
复制
<xsl:template match="homepage-updates" mode="updates">

然后相应地更改xsl:apply-templates

代码语言:javascript
复制
<xsl:apply-templates select="homepage-updates" mode="updates"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23122814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档