2012-09-10 2 views
-1

이 XML 파일이있어서 xsl/t를 통해 테이블로 변환해야합니다.xsl-TVGuide로 XML 표시하기

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

이 도움을 주셔서 대단히 감사합니다.

답변

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> 

다음은 XSL의 사용을주의 마 출력

<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을 : 중첩 너무 많이 피하기 위해 각각에 대해 - 코드를 사용하여 읽기 쉽도록하십시오.

당신이 주로 XSL을 사용하는 버전을 원하는 경우에 대한-각를,이 같은 출력을 생성한다 대신

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

이 XSLT를 사용해보십시오.xsl : apply-templates은 패턴 일치의 이점을 생성하고 xsl :을 대신 할 수있는 대안을 제공합니다.

+0

와우, 도움 주셔서 감사합니다. – dash

+0

xsl : for-each로 xslt 파일을 작성하려면 어떻게해야합니까? 도움을 감사하십시오. – dash

+0

내 대답에 xsl : for each 샘플을 추가했습니다! –