从当前组的访问属性第2部分

时间:2014-11-18 11:49:31

标签: xslt-2.0 xslt-grouping

对此问题的跟进Access attribute from within current-grouping-key() xslt

我有成千上万部电影,很多电影都是一样的 - 这里有示例代码

    <mediaList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="mediaList.xsd">
   <movie id="1125898" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>127 Hours</title>
   </movie>
   <movie id="1155300" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>Lord Jim</title>
   </movie>
   <movie id="866019" dateCreated="2014-09-18">
      <title>Othello</title>
</movie>
   <movie id="811875" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>Escape from New York</title>
   </movie>
   <movie id="1340523" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>Escape from L.A.</title>
   </movie>
   <movie id="1108660" dateCreated="2014-04-13" lastModified="2014-05-23">
      <title>Black Cat Run</title>
   </movie>
   <movie id="910246" dateCreated="2014-09-17">
      <title differentiator="1990">Othello</title>
</movie>
   <movie id="1324917" dateCreated="2014-04-13" lastModified="2014-05-28">
      <title>Police Story 2</title>
   </movie>
   <movie id="949534" dateCreated="2014-04-13" lastModified="2014-05-28">
      <title>Rambo: First Blood Part II</title>
   </movie>
  <movie id="910900" dateCreated="2014-09-14">
      <title differentiator="1965">Othello</title>
</movie>
</mediaList>

当我输入带有重复标题的电影时我努力将@differentiator添加到其中以便在排序时保持它们的独特性 - 但有时我可能会错过一个,所以我试图写一个模板找到所有相同的标题,然后打印任何不具有@differentiator的ANY。

CAN

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:output method="html" indent="yes"/>


    <xsl:template match="/">
        <html>
            <body>
                <ul>
 <xsl:apply-templates select="mediaList/movie[title[not(@differentiator)] = ./following-sibling::movie/title[not(@differentiator)]] | mediaList/movie[title[not(@differentiator)] = ./preceding-sibling::movie/title[not(@differentiator)]]"/>
</ul>
</body>
</html>
</xsl:template>


    <xsl:template match="movie">
        <li>
            <xsl:value-of select="concat(title[1],year,' appears multiple times')"/>
        </li>
    </xsl:template>

但有成千上万的电影......它效率很低!!

知道类似于链接的问题,有一些使用concat(。,&#39; _&#39;,@ differentialator)的潜行

但是输出将是出现不止一次的所有标题,而且还没有@differentiator

请指教!

1 个答案:

答案 0 :(得分:0)

XSLT 2.0有一个分组指令,所以请使用例如

<xsl:template match="/">
        <html>
            <body>
                <ul>
                  <xsl:for-each-group select="mediaList/movie" group-by="title">
                     <xsl:apply-templates select="current-group()[not(title/@differentiator)]"/>
</ul>
</body>
</html>
</xsl:template>