2012-11-27 2 views
2

다음은 변환해야 할 원본 XML 파일입니다. 그것은 내가 발행 한 앨범을 HTML 테이블로 변환해야하는 음악 앨범 레코드의 목록으로 발급 연도에 따라 오름차순으로 정렬되어 있습니다. 장르와 연결하고 각 레코드 요소에 포함 된 레코드 레이블 요소를 다음과 같이 연결해야합니다. id 참조를 만들고 각 앨범의 표에 출력합니다.정렬을 사용하여 XML에서 HTML로 변환

두 번째 변환에서 나는 기본적으로 동일한 작업을 수행해야하지만이 경우 출력에는 록 및 팝 장르의 앨범 만 포함되어야합니다. 내가 관리 할 수 ​​무엇

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="zadacha1.xsl" ?> 
<record_collection> 
    <genres> 
     <genre id="1">Rock</genre> 
     <genre id="2">Pop</genre> 
     <genre id="3">Disco</genre> 
     <genre id="4">Rap</genre> 
     <genre id="5">Electronic</genre> 
     <genre id="6">Country</genre> 
    </genres> 

    <record_labels> 
     <record_label id="1">DGC Records</record_label> 
     <record_label id="2">Atlantic</record_label> 
     <record_label id="3">Epic</record_label> 
     <record_label id="4">Warner Bros.</record_label> 
     <record_label id="5">EMI</record_label> 
     <record_label id="6">Columbia</record_label> 
    </record_labels> 

    <records> 
     <record> 
      <artist>Nirvana</artist> 
      <genreID>1</genreID> 
      <album>Nevermind</album> 
      <year_of_issue>1992</year_of_issue> 
      <record_labelID>1</record_labelID> 
     </record> 

     <record> 
      <artist>Twisted Sister</artist> 
      <genreID>1</genreID> 
      <album>Stay Hungry</album> 
      <year_of_issue>1984</year_of_issue> 
      <record_labelID>2</record_labelID> 
     </record> 

     <record> 
      <artist>Michael Jackson</artist> 
      <genreID>2</genreID> 
      <album>Thriller</album> 
      <year_of_issue>1982</year_of_issue> 
      <record_labelID>3</record_labelID> 
     </record> 

     <record> 
      <artist>Bee Gees</artist> 
      <genreID>3</genreID> 
      <album>Spirits Having Flown</album> 
      <year_of_issue>1979</year_of_issue> 
      <record_labelID>4</record_labelID> 
     </record> 

     <record> 
      <artist>Ice-T</artist> 
      <genreID>4</genreID> 
      <album>O.G. Original Gangster</album> 
      <year_of_issue>1991</year_of_issue> 
      <record_labelID>4</record_labelID> 
     </record> 

     <record> 
      <artist>Kraftwerk</artist> 
      <genreID>5</genreID> 
      <album>Computer World</album> 
      <year_of_issue>1981</year_of_issue> 
      <record_labelID>5</record_labelID> 
     </record> 

     <record> 
      <artist>Johnny Cash</artist> 
      <genreID>6</genreID> 
      <album>Man in Black</album> 
      <year_of_issue>1971</year_of_issue> 
      <record_labelID>6</record_labelID> 
     </record> 

    </records> 
</record_collection> 

출력이었다 비 분류 그들은 각각의 기록에 ID를 통해 연결되어 있기 때문에 장르와 레코드 레이블 요소없이 테이블과 내가 ID와 출력에 도달하는 방법을 모른다 이드 뒤에있는 텍스트. 여기 내 XSLT 변환으로 지금까지 무엇을 가지고 있습니다.

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/record_collection/records"> 
     <html> 
     <head> 
      <title>Records Collection</title> 
     </head> 
     <body> 
      <h1>Records Collection</h1> 
      <table border="1"> 
       <tr> 
        <th>Genre</th> 
        <th>Artist</th> 
        <th>Album</th> 
        <th>Year</th> 
        <th>Label</th> 
       </tr> 

       <xsl:apply-templates select="record" /> 
      </table> 
      <xsl:call-template name="footer" /> 
     </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="record"> 
     <tr> 
      <td>?</td> 
      <td><xsl:value-of select="artist" /></td> 
      <td><xsl:value-of select="album" /></td> 
      <td><xsl:value-of select="year_of_issue" /></td> 
      <td>?</td> 
     </tr> 
    </xsl:template> 


    <xsl:template name="footer"> 
     <div style="margin: 20px 0; padding: 10px; background-color: #efefef; "> 
      Record Collection 
     </div> 
    </xsl:template> 

</xsl:stylesheet> 

답변

2

당신은 사용해야합니다 :

<xsl:apply-templates select="record"> 
    <xsl:sort select="year_of_issue" order="ascending" data-type="number"/> 
</xsl:apply-templates> 

대신

<xsl:apply-templates select="record"/> 

의 테이블이 문제

+0

정말 고마워요! –

1

이 당신의 장르를 조회 할 xsl:key를 사용하기에 좋은 장소이며, 당신은 단지 락이나 팝에 의해 필터링하려면 기록

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/"> 
     <xsl:apply-templates select="/record_collection/records"></xsl:apply-templates> 
    </xsl:template> 

    <xsl:key name="genre-lookup" match="/record_collection/genres/genre" use="@id"/> 
    <xsl:key name="record_label" match="/record_collection/record_labels/record_label" use="@id"/> 

    <xsl:template match="records"> 
     <html> 
      <head> 
       <title>Records Collection</title> 
      </head> 
      <body> 
       <h1>Records Collection</h1> 
       <table border="1"> 
        <tr> 
         <th>Genre</th> 
         <th>Artist</th> 
         <th>Album</th> 
         <th>Year</th> 
         <th>Label</th> 
        </tr> 

        <xsl:apply-templates select="record" /> 
       </table> 
       <xsl:call-template name="footer" /> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="record"> 
     <tr> 
      <td> 
       <xsl:value-of select="key('genre-lookup',genreID)" /> 
      </td> 
      <td> 
       <xsl:value-of select="artist" /> 
      </td> 
      <td> 
       <xsl:value-of select="album" /> 
      </td> 
      <td> 
       <xsl:value-of select="year_of_issue" /> 
      </td> 
      <td> 
       <xsl:value-of select="key('record_label',record_labelID)" /> 
      </td> 
     </tr> 
    </xsl:template> 


    <xsl:template name="footer"> 
     <div style="margin: 20px 0; padding: 10px; background-color: #efefef; "> 
      Record Collection 
     </div> 
    </xsl:template> 

</xsl:stylesheet> 

레이블, 다음 genreId = 1/2 것만으로 필터링하여 apply-templates을 조정 :

<xsl:apply-templates select="record[genreID=1 or genreID=2]" /> 

기본 처리 명령이 실행되지 않도록 원본 xslt를 약간 변경하면 루트를 캡처 할 수 있습니다. g 문서에 적용됩니다.

+1

의 올해 ascendig 순서로 당신의 도움이 친구에 대한 큰 감사를 정렬됩니다 :) !!! 한 번만 더 - 변환 후에 문서 상단에 "장르"및 "레코드 레이블"범주의 항목이 남습니다. 변환 된 문서에 표시되지 않게하려면 어떻게합니까? –

+1

@ GrigorPetrov - 이것은 [기본 내장 템플릿] (http://stackoverflow.com/questions/3360017/why-does-xslt-output-all-text-by-default)의 영향입니다. 관심있는 노드들에게'apply-templates'를하는 'root'템플릿을 추가하면이 문제를 해결할 수 있습니다 (나의 대답에서 이것을 수행했습니다). – StuartLC

+0

그게 해결되었습니다! 시원한! –

관련 문제