0

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

xml 被删除。

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

我已经使用我在这里找到的解决方案解决了这个问题。这很简单。我的数据源将类别过滤为 {$ds-categories}。我将其更改为仅 38 并且效果很好。知道这将是一件简单的事情。

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

<?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-article 模板看起来像这样:

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

谢谢您的帮助!一旦我解决了这个问题,它肯定会帮助我避免遇到进一步的问题。

4

1 回答 1

1

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

<xsl:apply-templates select="homepage-updates/entry" mode="updates"/>

您也有与此匹配的模板

<xsl:template match="entry" mode="updates">

到目前为止,一切都很好。但是在这个“条目”模板中,你可以这样做

<xsl:for-each select="entry[@handle = updates]">

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

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

<xsl:template match="homepage-updates" mode="updates">

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

<xsl:apply-templates select="homepage-updates" mode="updates"/>
于 2014-04-17T07:53:49.257 回答