-1

この XML ファイルがあり、xsl/t を介してテーブルに変換する必要があります。

<?xml version="1.0" encoding="ISO-8859-1"?>

<TVGuide start="2001-07-05" end="2001-07-05">
  <Channel>
    <Name>BBC1</Name>
    <Program>
      <Start>2001-07-05T19:00:00</Start>
      <Duration>PT30M</Duration>
      <Series>A QuestionOfSport</Series>
      <Title></Title>
    </Program>
    <Program rating="5" flag="favorite">
      <Start>2001-07-05T19:30:00</Start>
      <Duration>PT30M</Duration>
      <Series>EastEnders</Series>
      <Title></Title>
      <Description>
        Mark's health scare forces him to reconsider his future with Lisa,
        while Jamie is torn between Sonia and Zoe.
      </Description>
      <CastList>
        <CastMember>
          <Character><Name>Zoe Slater</Name></Character>
          <Actor><Name>Michelle Ryan</Name></Actor>
        </CastMember>
        <CastMember>
          <Character><Name>Jamie Mitchell</Name></Character>
          <Actor><Name>Jack Ryder</Name></Actor>
        </CastMember>
        <CastMember>
          <Character><Name>Sonia Jackson</Name></Character>
          <Actor><Name>Natalie Cassidy</Name></Actor>
        </CastMember>
      </CastList>
      <Writers>
        <Writer><Name>Nick Saltrese</Name></Writer>
        <Writer><Name>Julie Wassmer</Name></Writer>
      </Writers>
      <Director><Name>Stewart Edwards</Name></Director>
      <Producer><Name>Emma Turner</Name></Producer>
    </Program>
    <Program type="documentary">
      <Start>2001-07-05T20:00:00</Start>
      <Duration>PT45M</Duration>
      <Series></Series>
      <Title>Get Real with Casualty</Title>
    </Program>
    <Program>
      <Start>2001-07-05T20:45:00</Start>
      <Duration>PT45M</Duration>
      <Series>Lottery</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T21:30:00</Start>
      <Duration>PT1H</Duration>
      <Series>Panorama</Series>
      <Title></Title>
    </Program>
  </Channel>
  <Channel>
    <Name>BBC2</Name>
    <Program>
      <Start>2001-07-05T19:00:00</Start>
      <Duration>PT1H</Duration>
      <Series>Snooker</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T20:00:00</Start>
      <Duration>PT1H</Duration>
      <Series>HomeFront</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T21:00:00</Start>
      <Duration>PT50M</Duration>
      <Series>WildAfrica</Series>
      <Title></Title>
    </Program>
    <Program flag="interesting">
      <Start>2001-07-05T21:50:00</Start>
      <Duration>PT40M</Duration>
      <Series>Taboo</Series>
      <Title>Nakedness</Title>
    </Program>
  </Channel>
  <Channel>
    <Name>ITV</Name>
    <Program>
      <Start>2001-07-05T19:00:00</Start>
      <Duration>PT30M</Duration>
      <Series>Emmerdale</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T19:30:00</Start>
      <Duration>PT30M</Duration>
      <Series>CoronationStreet</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T20:00:00</Start>
      <Duration>PT1H</Duration>
      <Series>Millionaire</Series>
      <Title></Title>
    </Program>
    <Program type="drama">
      <Start>2001-07-05T21:00:00</Start>
      <Duration>PT2H</Duration>
      <Series></Series>
      <Title>Hot Money</Title>
    </Program>
  </Channel>
  <Channel>
    <Name>Channel 4</Name>
    <Program>
      <Start>2001-07-05T19:00:00</Start>
      <Duration>PT55M</Duration>
      <Series>Channel4News</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T19:55:00</Start>
      <Duration>PT5M</Duration>
      <Series>SlotArt</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T20:00:00</Start>
      <Duration>PT30M</Duration>
      <Series>Brookside</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T20:30:00</Start>
      <Duration>PT30M</Duration>
      <Series>Brookside</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T21:00:00</Start>
      <Duration>PT1H</Duration>
      <Series>Swallow</Series>
      <Title></Title>
    </Program>
    <Program flag="favorite">
      <Start>2001-07-05T22:00:00</Start>
      <Duration>PT1H</Duration>
      <Series>AllyMcBeal</Series>
      <Title></Title>
    </Program>
  </Channel>
  <Channel>
    <Name>Channel 5</Name>
    <Program>
      <Start>2001-07-05T19:00:00</Start>
      <Duration>PT30M</Duration>
      <Series>MovieChartShow</Series>
      <Title></Title>
    </Program>
    <Program>
      <Start>2001-07-05T19:30:00</Start>
      <Duration>PT30M</Duration>
      <Series>FiveNews</Series>
      <Title></Title>
    </Program>
    <Program type="entertainment">
      <Start>2001-07-05T20:00:00</Start>
      <Duration>PT1H</Duration>
      <Series></Series>
      <Title>The World's Worst Drivers Caught On Tape</Title>
    </Program>
    <Program type="film">
      <Start>2001-07-05T21:00:00</Start>
      <Duration>PT1H55M</Duration>
      <Series></Series>
      <Title>Black and White</Title>
    </Program>
  </Channel>
</TVGuide>

おそらく問題は xsl ファイルにあると思います。私は専門家ではありません。あなたの助けに感謝します。

ここにxslファイルがあります。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="hatchala" match="/TVGuide/Channel/Program" use="Start"/>
  <xsl:template match="/">

        <table border="1">

            <tr>hours
              <xsl:call-template name="tvchan"/>
            </tr>
            <tr>
              <th>
              <xsl:call-template name="hour"/>
              </th>
            </tr>

        </table> 
  </xsl:template>
  <xsl:template name="tvchan">
    <xsl:for-each select="/TVGuide/Channel">

      <th>
        <td>
          <xsl:value-of select ="Name"/>
        </td>

      </th>
    </xsl:for-each>

  </xsl:template>
  <xsl:template name="hour">
          <xsl:for-each  select="/TVGuide/Channel/Program[not(./Start=following::Start)]">
            <xsl:sort select="Start"/>
            <xsl:variable name="zman" select ="Start"/>
            <tr>
              <td>
                <xsl:value-of select="Start"/>
              </td>

              <xsl:for-each  select="/TVGuide/Channel/Program[Start=$zman]">
                <xsl:variable name="name1">
                  <xsl:choose>
                    <xsl:when test="Title!=''">
                      <xsl:value-of select="Title"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="Series"/>
                    </xsl:otherwise>

                  </xsl:choose>
                </xsl:variable>
                <td>
                  <xsl:value-of select="$name1"/>

                </td>
          </xsl:for-each>
            </tr>
          </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

結果には、すべてのプログラムと時間の表が表示されます。

http://dl.dropbox.com/u/99771726/table.png

助けてくれてありがとう。

4

1 に答える 1

2

まず、XSLT1.0を使用している場合は、Muenchian Groupingと呼ばれる手法を使用して、すべてのプログラムの明確な開始時刻を取得できます。これには、開始要素によってプログラムを検索するためのキーを設定することが含まれます。

<xsl:key name="hatchala" match="Program" use="Start"/>

キーは任意のレベルでそれらを検索するため、必ずしもプログラム要素への完全なXpathは必要ないことに注意してください(異なるレベルにそのような要素があり、それらを制限したい場合にのみ、物事を定量化します)。

次に、明確な開始時刻を取得するために、開始時刻のキーでその要素が最初に出現したプログラムを検索します。

<xsl:apply-templates 
   select="/TVGuide/Channel/Program
      [generate-id() = generate-id(key('hatchala', Start)[1])]" mode="header">

(最終的なXSLTはプログラム要素と複数回一致するため、「モード」の使用はまもなく明らかになります)。

また、特定の開始時間に特定のチャネルのプログラムを検索するためのキーを定義します

<xsl:key name="hatchala2" match="Program" use="concat(../Name, '|', Start)"/>

次に、指定された開始時間の間、次のようにすべてのチャネルをループします。

<xsl:apply-templates select="/TVGuide/Channel" mode="program">
    <xsl:with-param name="Start" select="Start"/>
</xsl:apply-templates>

そして、これに一致するテンプレートは、キーを使用してプログラムを検索できます

<xsl:template match="Channel" mode="program">
   <xsl:param name="Start"/>
   <td>
      <xsl:apply-templates select="key('hatchala2', concat(Name, '|', $Start))" mode="program"/>
   </td>
</xsl:template>

これが完全なXSLTです

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes"/>
   <xsl:key name="hatchala" match="Program" use="Start"/>
   <xsl:key name="hatchala2" match="Program" use="concat(../Name, '|', Start)"/>

   <xsl:template match="/TVGuide">
      <table border="1">
         <tr>
            <th>hours</th>
            <xsl:apply-templates select="Channel" mode="header"/>
         </tr>
         <tr>
            <th>
               <xsl:apply-templates select="/TVGuide/Channel/Program[generate-id() = generate-id(key('hatchala', Start)[1])]" mode="header">
                  <xsl:sort select="Start"/>
               </xsl:apply-templates>
            </th>
         </tr>
      </table>
   </xsl:template>

   <xsl:template match="Channel" mode="header">
      <th>
         <xsl:value-of select="Name"/>
      </th>
   </xsl:template>

   <xsl:template match="Program" mode="header">
      <tr>
         <td>
            <xsl:value-of select="Start"/>
         </td>
         <xsl:apply-templates select="/TVGuide/Channel" mode="program">
            <xsl:with-param name="Start" select="Start"/>
         </xsl:apply-templates>
      </tr>
   </xsl:template>

   <xsl:template match="Channel" mode="program">
      <xsl:param name="Start"/>
      <td>
         <xsl:apply-templates select="key('hatchala2', concat(Name, '|', $Start))" mode="program"/>
      </td>
   </xsl:template>

   <xsl:template match="Program" mode="program">
      <xsl:choose>
         <xsl:when test="Title!=''">
            <xsl:value-of select="Title"/>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="Series"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

次に、以下が出力されます

<table border="1">
   <tr>
      <th>hours</th>
      <th>BBC1</th>
      <th>BBC2</th>
      <th>ITV</th>
      <th>Channel 4</th>
      <th>Channel 5</th>
   </tr>
   <tr>
      <th>
         <tr>
            <td>2001-07-05T19:00:00</td>
            <td>A QuestionOfSport</td>
            <td>Snooker</td>
            <td>Emmerdale</td>
            <td>Channel4News</td>
            <td>MovieChartShow</td>
         </tr>
         <tr>
            <td>2001-07-05T19:30:00</td>
            <td>EastEnders</td>
            <td/>
            <td>CoronationStreet</td>
            <td/>
            <td>FiveNews</td>
         </tr>
         <tr>
            <td>2001-07-05T19:55:00</td>
            <td/>
            <td/>
            <td/>
            <td>SlotArt</td>
            <td/>
         </tr>
         <tr>
            <td>2001-07-05T20:00:00</td>
            <td>Get Real with Casualty</td>
            <td>HomeFront</td>
            <td>Millionaire</td>
            <td>Brookside</td>
            <td>The World's Worst Drivers Caught On Tape</td>
         </tr>
         <tr>
            <td>2001-07-05T20:30:00</td>
            <td/>
            <td/>
            <td/>
            <td>Brookside</td>
            <td/>
         </tr>
         <tr>
            <td>2001-07-05T20:45:00</td>
            <td>Lottery</td>
            <td/>
            <td/>
            <td/>
            <td/>
         </tr>
         <tr>
            <td>2001-07-05T21:00:00</td>
            <td/>
            <td>WildAfrica</td>
            <td>Hot Money</td>
            <td>Swallow</td>
            <td>Black and White</td>
         </tr>
         <tr>
            <td>2001-07-05T21:30:00</td>
            <td>Panorama</td>
            <td/>
            <td/>
            <td/>
            <td/>
         </tr>
         <tr>
            <td>2001-07-05T21:50:00</td>
            <td/>
            <td>Nakedness</td>
            <td/>
            <td/>
            <td/>
         </tr>
         <tr>
            <td>2001-07-05T22:00:00</td>
            <td/>
            <td/>
            <td/>
            <td>AllyMcBeal</td>
            <td/>
         </tr>
      </th>
   </tr>
</table>

ネストされたコードが多すぎるのを避け、読みやすくするために、 xsl:for-eachの代わりにxsl:apply-templatesを使用していることに注意してください。

主にxsl:for-eachを使用するバージョンが必要な場合は、代わりにこのXSLTを試してください

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes"/>
   <xsl:key name="hatchala" match="Program" use="Start"/>
   <xsl:key name="hatchala2" match="Program" use="concat(../Name, '|', Start)"/>

   <xsl:template match="/TVGuide">
      <table border="1">
         <tr>
            <th>hours</th>
            <xsl:for-each select="Channel">
               <th>
                  <xsl:value-of select="Name"/>
               </th>
            </xsl:for-each>
         </tr>
         <tr>
            <th>
               <xsl:for-each select="/TVGuide/Channel/Program[generate-id() = generate-id(key('hatchala', Start)[1])]">
                  <xsl:sort select="Start"/>

                  <xsl:variable name="Start" select="Start" />
                  <tr>
                     <td>
                        <xsl:value-of select="$Start"/>
                     </td>
                     <xsl:for-each select="/TVGuide/Channel">
                        <td>
                           <xsl:apply-templates select="key('hatchala2', concat(Name, '|', $Start))" />
                        </td>
                     </xsl:for-each>
                  </tr>                  
               </xsl:for-each>
            </th>
         </tr>
      </table>
   </xsl:template>

   <xsl:template match="Program">
      <xsl:value-of select="Series"/>
   </xsl:template>

   <xsl:template match="Program[Title!='']">
      <xsl:value-of select="Title"/>
   </xsl:template>
</xsl:stylesheet>

これにより、同じ出力が生成されます。xsl:apply-templatesを1つ残していることに注意してください。これは、パターンマッチングの利点を生み出し、 xsl:chooseの代替手段を提供するためです。

于 2012-09-11T13:05:27.433 に答える