本文介绍了嵌套XSL for-each语句的难度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是XSL的新手,工作有点超出我的头脑.

I am new to XSL and working a bit above my head.

我有一个XML文件(如下所示),我想按月和年分组.我是通过 Muenchian分组实现的...但是导致围绕嵌套的for-each语句的新问题.

I have an xml file (show below) which I wanted to group by month&year. I achieved this with Muenchian Grouping... however that led to a new issue around nested for-each statements.

问题在于,所有链接现在仅指向数组中的第一个链接,而不是它们自己的链接.

The issue is that all links are now only pointing to the first link in the array, rather than their own.

我的XML下面是XSL,它有两个正文语句,由///

Below my XML is the XSL which has two body statements separated by ///

首先是我对分组和不正确链接的逻辑.最初被注释掉的下一个是我在分组之前的声明,该分组具有正确起作用的链接.

The first is my logic with the grouping, and incorrect linking. The lower one, which is initially commented out, was my statement before the grouping which has correctly functioning links.

如果有人可以帮助我重组我的逻辑,将不胜感激.我真的快要完成了!

If somebody could please help me to restructure my logic it would be very very appreciated. I'm so close to finishing!

XML:

<?xml version="1.0" encoding="UTF-8"?>
-<rss version="2.0">-
 <channel>
  <title>Company Name</title>
  <description>Company News and Events</description>
  <link>http://brattle.com/</link>
    <item>
      <title>Paul Hinton and Pavitra Kumar to Speak at High-Frequency Trading Litigation Webinar</title>
      <description><p>Brattle principal Paul Hinton and senior associate Pavitra Kumar will present in the upcoming webinar, &ldquo;High-Frequency Trading Litigation in 2015,&rdquo; hosted by The Knowledge Group on March 26, 2015 from 3:00 pm – 5:00 pm EST.</p><p>This webcast will discuss issues surrounding High-Frequency Trading (HFT), which in recent years has increased and become a focus of regulators. The Joint Commodity Futures Trading Commission (CFTC)-Securities and Exchange Commission (SEC) Advisory Committee has expressed concern over the volume of orders and cancellations of trades (spoofing), and the SEC, CFTC, and the FBI have all announced investigations of HFT practices. Panelists will address how HFT will continue to create challenging analytical and legal issues involving its complexity and multi-party nature of trading, changing regulation, and new legal theories.</p><p>Dr. Kumar will speak on the &ldquo;Key Areas of High-Frequency-Trading-Related Litigation and Related Economic Issues,&rdquo; including exchange-related litigation, litigation against HFT firms, and market access litigation. Mr. Hinton will speak on the latest issues surrounding HFT litigation in light of the continued scrutiny by regulators placed on financial firms.</p><p>For more information on the webinar, or to register, please visit the <a href="http://theknowledgegroup.org/event_name/high-frequency-trading-litigation-in-2015-explored-live-webcast/">website</a>.</p></description>
      <pubDate>26 Mar 2015</pubDate>
      <link>http://brattle.com/news-and-knowledge/events/813</link>
      <guid>http://brattle.com/news-and-knowledge/events/813</guid>
    </item>
    <item>
      <title>Bruce Tsuchida to Present at Upcoming LSI Conference on Transmission in the Northeast</title>
      <description><p>Brattle principal Bruce Tsuchida has been selected to present at the upcoming Law Seminars International (LSI) conference, &ldquo;Transmission in the Northeast: Legal, Business and Regulatory Developments,&rdquo; taking place March 19-20, 2015 in Boston, MA.</p><p>Participants in this event will include attorneys, industry executives, government officials, and customer representatives involved with electric generation and transmission. The conference will examine the issues surrounding transmission and distributed system technology and policy, along with the regulatory changes that are affecting expansion, coordination, and planning efforts.</p><p>Mr. Tsuchida will participate in the panel session, &ldquo;How Greater Reliance on Distributed Resources Affects Distribution and Transmission.&rdquo;</p><p>To learn more about the event, or to register, visit the LSI <a href="http://www.lawseminars.com/detail.php?SeminarCode=15ETRANMA">website</a>.</p></description>
      <pubDate>19 Mar 2015</pubDate>
      <link>http://brattle.com/news-and-knowledge/events/814</link>
      <guid>http://brattle.com/news-and-knowledge/events/814</guid>
    </item>
    <item>
      <title>Deploying Up to 5,000 MW of Grid-Integrated Electricity Storage in Texas Could Provide Substantial Net Benefits According to Brattle Economists</title>
      <description><p>A report recently released by Brattle economists finds that the deployment of grid-integrated distributed electricity storage in Texas could provide substantial net benefits to the power system in ERCOT and its electricity customers. The report, commissioned by Oncor Electric Delivery Company, a Transmission and Distribution Service Provider (TDSP) in Texas, evaluates whether storage could be cost-effectively deployed from the perspectives of retail customers, wholesale electricity market participants, and the combined system or &ldquo;society as a whole&rdquo; while maintaining wholesale power prices that continue to support necessary generation investments. The recently released report is a follow-up to preliminary results released in November 2014.</p><p>The Brattle report estimates that 3,000 to 5,000 MW of grid-integrated, distributed electricity storage would be the most cost-effective from an ERCOT system-wide societal perspective based on a forecast of installed cost of storage of approximately $350/kWh. The authors identify four components of storage value from a system-wide perspective to estimate net benefits, including avoided distribution outages, deferred transmission and distribution (T&D) investment, avoided new generation or demand-side capacity investments, and electricity production cost savings. From an electricity customer&rsquo;s perspective, deploying storage across ERCOT would decrease customer bills slightly and additionally provide significant reliability benefits in the form of reduced power outages for customers located in areas where storage is installed.</p><p>&ldquo;Considering both the impact on electricity bills and improved reliability of grid-integrated storage, the customer benefits would significantly exceed costs,&rdquo; said Judy Chang, a Brattle principal and lead author of the study. &ldquo;However, while beneficial from an integrated, system-wide perspective, an efficient scale of storage deployment would not be reached if deployed by merchant developers who rely solely on participation in the wholesale market, or by retail customers who use it solely for back-up power, or by wires companies who deploy it solely for capturing T&D benefits. These entities, independently and separately, will not be able to capture the full value of the storage to viably support the magnitude of investment that could be cost-effective for ERCOT.&rdquo;</p><p>The authors show that enabling cost-effective investments in electricity storage will require a regulatory framework that helps investors capture the combined values of storage from the wholesale market, the T&D systems, and customer outage reduction. Thus, the authors recommend a regulatory framework that would involve allowing the transmission and distribution companies to deploy the electricity storage on the distribution system and &ldquo;auction off&rdquo; to independent third parties the rights to use the storage facilities for participation in the wholesale market.</p><p>&ldquo;The Value of Distributed Electricity Storage in Texas: Proposed Policy for Enabling Grid-Integrated Storage Investments,&rdquo; is authored by Ms. Chang, Johannes Pfeifenberger, Kathleen Spees, and Matthew Davis, with the support of Brattle research analysts. It is available for download below.</p></description><pubDate>13 Mar 2015</pubDate>
      <link>http://brattle.com/news-and-knowledge/news/808</link>
      <guid>http://brattle.com/news-and-knowledge/news/808</guid>
    </item>
  </channel>
</rss>

XSL:

<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
           version="1.0" exclude-result-prefixes="xsl ddwrt msxsl rssaggwrt"
           xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
           xmlns:rssaggwrt="http://schemas.microsoft.com/WebParts/v3/rssagg/runtime"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
           xmlns:rssFeed="urn:schemas-microsoft-com:sharepoint:RSSAggregatorWebPart">

<xsl:param name="rss_FeedLimit">30</xsl:param>
<xsl:param name="rss_ExpandFeed">true</xsl:param>
<xsl:param name="rss_LCID">1033</xsl:param>
<xsl:param name="rss_WebPartID">RSS_Viewer_WebPart</xsl:param>
<xsl:param name="rss_alignValue">left</xsl:param>
<xsl:param name="rss_IsDesignMode">True</xsl:param>



<!--////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////-->



<xsl:key name="item-pubDate" match="item" use="substring(pubDate,3,9)" />
<xsl:template match="channel">
<xsl:variable name="Rows" select="channel/item"/>
<xsl:variable name="RowCount" select="count($Rows)"/>
<xsl:variable name="item_link" select="item/link"/>
<xsl:variable name="item_title" select="description"/>

<xsl:for-each select="$Rows"/>
<xsl:variable name="CurPosition" select="position()" />
<xsl:variable name="PrevPosition" select="position()-1" />
<xsl:variable name="RssFeedLink" select="$rss_WebPartID" />
<xsl:variable name="CurrentElement" select="concat($RssFeedLink,$CurPosition)" />

<xsl:for-each select ="item[count(. | key('item-pubDate', substring(pubDate,3,9))[1]) = 1]">
 <div align="center">
    <hr/>
  <xsl:value-of select="substring(pubDate,3,9)" />
 </div>
 <xsl:for-each select="key('item-pubDate', substring(pubDate,3,9))">
   <!--<xsl:sort select="pubDate" order="descending"/>-->
   <li>
     <xsl:for-each select="$Rows"/>
        <a href="{$item_link}" title="{$item_title}">
          <xsl:variable name="SafeHtml">
            <xsl:call-template name="GetSafeHtml">
              <xsl:with-param name="Html" select="title"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="$SafeHtml" disable-output-escaping="yes"/>
        </a>
     <!--</xsl:for-each>-->
  </li>
 </xsl:for-each>
</xsl:for-each>
</xsl:template>



<!--////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////-->



 <!--<xsl:template match="item">
  <xsl:variable name="Rows" select="channel/item"/>
  <xsl:variable name="RowCount" select="count($Rows)"/>
  <xsl:variable name="item_link" select="link"/>
  <xsl:variable name="item_title" select="description"/>

  <xsl:for-each select="$Rows"/>
  <xsl:variable name="CurPosition" select="position()" />
  <xsl:variable name="PrevPosition" select="position()-1" />
  <xsl:variable name="RssFeedLink" select="$rss_WebPartID" />
  <xsl:variable name="CurrentElement" select="concat($RssFeedLink,$CurPosition)" />
  <xsl:if test="not(substring(pubDate,3,9) = 0)">
    <div align="center">
      <hr/>
      <xsl:value-of select="substring(pubDate,3,9)" disable-output-escaping="yes"/>
    </div>
  </xsl:if>
  <li>
    <a href="{$item_link}" title="{$item_title}">
      <xsl:variable name="SafeHtml">
        <xsl:call-template name="GetSafeHtml">
          <xsl:with-param name="Html" select="title"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$SafeHtml" disable-output-escaping="yes"/>
    </a>
  </li>
</xsl:template>-->



<!--////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////-->



<xsl:template name="GetSafeHtml">
<xsl:param name="Html"/>
<xsl:choose>
  <xsl:when test="$rss_IsDesignMode = 'True'">
    <xsl:value-of select="$Html"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="rssaggwrt:MakeSafe($Html)"/>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

由于此请求的性质,我保留了网址等内容.我相信你,互联网.

Due to the nature of this request, I left the urls etc in tact. I'm trusting you, internet.

哈哈,非常感谢您的帮助,在此期间,我将继续摆弄我的嵌套

haha, any help very appreciated, i'll keep fiddling with my nesting in the meantime

推荐答案

请参考,如果我从上一个问题中获取了经过稍微调整的XML输入:

Fo reference, if I take a slightly adjusted XML input from your previous question:

XML

<rss version="2.0">
<channel>
  <title>Company Name</title>
  <description>Company News and Events</description>
  <link>http://website.com/</link>
  <item>
    <title>Article 1</title>
      <description><p>Something</p></description>
      <pubDate>12 Jan 2015</pubDate>
      <link>http://website.com/news-and-knowledge/news/807</link>
      <guid>http://website.com/news-and-knowledge/news/807</guid>
  </item>
  <item>
    <title>Article 2</title>
      <description><p>Something</p></description>
      <pubDate>20 Dec 2014</pubDate>
      <link>http://website.com/news-and-knowledge/news/806</link>
      <guid>http://website.com/news-and-knowledge/news/806</guid>
  </item>
  <item>
    <title>Article 3</title>
      <description><p>Something</p></description>
      <pubDate>09 Dec 2014</pubDate>
      <link>http://website.com/news-and-knowledge/news/805</link>
      <guid>http://website.com/news-and-knowledge/news/805</guid>
  </item>
  </channel>
</rss>

并应用以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="item-by-month" match="item" use="substring(pubDate, 4)" />

<xsl:template match="/rss">
    <div>
        <xsl:apply-templates select="channel"/>
    </div>
</xsl:template>

<xsl:template match="channel">
    <!-- channel title -->
    <h2><xsl:value-of select="title"/></h2>
    <!-- for each distinct month -->
    <xsl:for-each select="item[count(. | key('item-by-month', substring(pubDate, 4))[1]) = 1]">
        <!-- month title -->
        <h3><xsl:value-of select="substring(pubDate, 4)"/></h3>
        <ul>
            <!-- list the items in this month -->
            <xsl:apply-templates select="key('item-by-month', substring(pubDate, 4))"/>
        </ul>
    </xsl:for-each>
</xsl:template>

<xsl:template match="item">
    <li>
        <a href="{link}" title="{title}">
            <xsl:value-of select="title"/>
        </a>
    </li>
</xsl:template>

</xsl:stylesheet>

结果将是:

<div>
   <h2>Company Name</h2>
   <h3>Jan 2015</h3>
   <ul>
      <li>
         <a href="http://website.com/news-and-knowledge/news/807" title="Article 1">Article 1</a>
      </li>
   </ul>
   <h3>Dec 2014</h3>
   <ul>
      <li>
         <a href="http://website.com/news-and-knowledge/news/806" title="Article 2">Article 2</a>
      </li>
      <li>
         <a href="http://website.com/news-and-knowledge/news/805" title="Article 3">Article 3</a>
      </li>
   </ul>
</div>

呈现为:

如果您有更多的代码,请确保有充分的理由.

If you have any code more than that, make sure you have a good reason for it.

还请注意,假设pubDate使用"dd-mmm-yyy"(即日期数字填充为两位数字)格式.

Note also the assumption that pubDate uses the "dd-mmm-yyy" (i.e. day numbers padded to two digits) format.

这篇关于嵌套XSL for-each语句的难度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 16:47