2012-03-01 18 views
6

Vì vậy, tôi đã cố gắng để nghiên cứu này và tôi gần như kết thúc trí thông minh của tôi. Tôi đã tìm cách nhúng XSL vào XML trên dpawson.co.uk, nhưng tôi không thể tìm ra những gì tôi đang làm sai. Tôi đã lùng sục internet cố gắng tìm giải pháp và giải thích, nhưng không ai có câu trả lời.Nhúng XSL vào XML để hiển thị trong Trình duyệt web: Tôi đang làm gì sai?

Tôi đang cố gắng tạo một tệp độc lập với máy chủ để tôi có thể gửi một tệp tới đồng nghiệp của mình với hy vọng họ có thể xem tệp bằng cách mở tệp đó trong trình duyệt web của họ.

Dưới đây là mã của tôi cho đến nay:

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

<!--Start XSL--> 

<?xml-stylesheet type="text/xml" href="#stylesheet"?> 

<!DOCTYPE doc [ 
<!ATTLIST xsl:stylesheet 
id ID #REQUIRED> 
]> 
<doc> 

<xsl:stylesheet id="stylesheet" 
       version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:template match="xsl:stylesheet" /> 
    <!--Variables--> 
    <xsl:variable name="a" select="list/movie/seen[@value ='Yes']" /> 
    <xsl:variable name="b" select="list/movie/seen" /> 
    <xsl:variable name="c" select="sum(list/movie/rating/@value)" /> 
    <xsl:variable name="d" select="$c div count($a)" /> 
    <xsl:variable name="e" select="count($a) div count($b)" /> 
    <xsl:variable name="f" select="list/movie/seen[@value ='No']" /> 
    <xsl:variable name="g" select="list/movie/seen[@value ='Prior']" /> 
    <xsl:variable name="h" select="count($f) div count($b)" /> 
    <xsl:variable name="j" select="count($g) div count($b)" /> 
    <xsl:variable name="minutes_total" select="sum(list/movie/length[@value ='y'])" /> 
    <xsl:variable name="minutes" select="$minutes_total mod 60" /> 
    <xsl:variable name="hours" select="floor($minutes_total div 60) mod 24" /> 
    <xsl:variable name="hours2" select="floor($minutes_total div 60)" /> 
    <xsl:variable name="days" select="floor($hours2 div 24)" /> 
    <xsl:decimal-format name="percent" /> 
    <xsl:decimal-format name="average" decimal-separator="." /> 
    <!--End Variables--> 
    <xsl:template match="/doc"> 
     <html> 
      <head> 
       <style> 
        h2{ 
        font-family: Courier, Courier New, monospace; 
        font-size: 32px; 
        text-decoration: underline; 
        } 
        body{ 
        font-family: Courier New, monospace; 
        } 
        p{ 
        font-size: 16px; 
        } 
        table{ 
        font-size: 14px; 
        } 
        .title{ 
        text-align:left; 
        } 
        .release{ 
        text-align:center; 
        } 
        .seen{ 
        text-align:center; 
        } 
        .rating{ 
        text-align:right; 
        } 
        .length{ 
        text-align:center; 
        } 
       </style> 
      </head> 
      <body> 
       <h2>My Movie List</h2> 
       <p>Movies seen so far: <xsl:value-of select="count($a)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($e, '#%', 'percent')" /><br /> 
       Movies yet to see: <xsl:value-of select="count($f)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($h, '#%', 'percent')" /><br /> 
       Movies seen prior to making list: <xsl:value-of select="count($g)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($j, '#%', 'percent')" /><br /> 
       Total time watched: <xsl:value-of select="format-number($days, '#0')" /> days, <xsl:value-of select="format-number($hours, '#0')" /> hours, <xsl:value-of select="format-number($minutes, '#0')" /> minutes<br /> 
       Average rating: <xsl:value-of select="format-number($d, '#.000', 'average')" /> stars out of 5</p> 
       <br /> 
       <table border="1"> 
        <tr> 
         <th>#</th> 
         <th>Title</th> 
         <th>Release Date</th> 
         <th>Length</th> 
         <th>Seen</th> 
         <th>Rating</th> 
        </tr> 
        <xsl:for-each select="list/movie"> 
         <xsl:choose> 
          <xsl:when test='seen = "Yes"'> 
           <tr style="background-color:#666; color:#fff"> 
            <td> <xsl:number /></td> 
            <td class="title"><xsl:value-of select="title"/></td> 
            <td class="release"><xsl:value-of select="release"/></td> 
            <td class="length"><xsl:value-of select="length" /> minutes</td> 
            <td class="seen"><xsl:value-of select="seen"/></td> 
            <td class="rating"><xsl:value-of select="rating"/></td> 
           </tr> 
          </xsl:when> 
          <xsl:when test='seen = "Seen prior to making list"'> 
           <tr style="background-color:#999; color:#000"> 
            <td> <xsl:number /></td> 
            <td class="title"><xsl:value-of select="title"/></td> 
            <td class="release"><xsl:value-of select="release"/></td> 
            <td class="length"><xsl:value-of select="length"/> minutes</td> 
            <td class="seen"><xsl:value-of select="seen"/></td> 
            <td class="rating"><xsl:value-of select="rating"/></td> 
           </tr> 
          </xsl:when> 
          <xsl:otherwise> 
           <tr style="background-color:#fff;"> 
            <td> <xsl:number /></td> 
            <td class="title"><xsl:value-of select="title"/></td> 
            <td class="release"><xsl:value-of select="release"/></td> 
            <td class="length"><xsl:value-of select="length" /> minutes</td> 
            <td class="seen"><xsl:value-of select="seen"/></td> 
            <td class="rating"><xsl:value-of select="rating"/></td> 
           </tr> 
          </xsl:otherwise> 
         </xsl:choose> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 
<!--Start XML--> 
<list> 
    <movie> 
     <title>2001: A Space Odyssey</title> 
     <release>1968</release> 
     <seen value="No">No</seen> 
     <rating>N/A</rating> 
     <length value="n">141</length> 
    </movie> 
    <movie> 
     <title>28 Days Later</title> 
     <release>2002</release> 
     <seen value="No">No</seen> 
     <rating>N/A</rating> 
     <length value="n">113</length> 
    </movie> 
    <movie> 
     <title>28 Weeks Later</title> 
     <release>2007</release> 
     <seen value="No">No</seen> 
     <rating>N/A</rating> 
     <length value="n">100</length> 
    </movie> 
    <movie> 
     <title>A Clockwork Orange</title> 
     <release>1971</release> 
     <seen value="Yes">Yes</seen> 
     <rating value="2">&#9734;&#9734;&#9734;&#9733;&#9733;</rating> 
     <length value="y">136</length> 
    </movie> 
<!--Rest of XML--> 
</list> 
</doc> 

Đó là một danh sách các phim có chứa bao nhiêu tôi đã nhìn thấy cho đến nay, có bao nhiêu tôi chưa thấy, có bao nhiêu tôi đã nhìn thấy trước khi tôi tạo danh sách, tổng thời gian tôi đã đưa vào xem phim và xếp hạng trung bình cho mỗi bộ phim được xem.

Vấn đề là tất cả các biến được hiển thị với giá trị bằng không, điều này gây ra chia cho số không cho giá trị NaN trên một số đầu ra.

Bất cứ điều gì mà bất kỳ ai cũng có thể cung cấp đều sẽ được đánh giá cao!

+0

Tôi không thực sự hiểu những gì bạn đang cố gắng làm ở đây nhưng tôi không nghĩ rằng đây là một bản định kiểu. Thẻ của bạn được nhúng trong một thẻ rất lạ – Kevan

+0

Thật không may, XML của tôi quá lớn để vừa hoàn toàn trong phần nội dung của câu hỏi. Những gì tôi đang cố gắng làm là biến các biến của tôi thành công việc. Khi tôi có các tệp .xml và .xsl riêng biệt, nó hoạt động rất đẹp. Nhưng khi tôi nhúng, xml xuất hiện, nhưng các biến của tôi không hoạt động. Tôi nhận được rất nhiều 0 và NaN. – ishmael624

+1

Bạn đang làm gì với tài liệu này. Bạn nói "xml xuất hiện". Ở đâu? Bạn đang chạy phần mềm gì và làm cách nào? –

Trả lời

8

Các mã sau đây:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="#stylesheet"?> 
<!DOCTYPE doc [ 
<!ATTLIST xsl:stylesheet 
id ID #REQUIRED> 
]> 
<doc> 
<!--Start XSL--> 
<xsl:stylesheet id="stylesheet" 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 

    <xsl:template match="xsl:stylesheet" /> 

    <!--Variables--> 
    <xsl:variable name="a" select="/doc/list/movie/seen[@value ='Yes']" /> 
    <xsl:variable name="b" select="/doc/list/movie/seen" /> 
    <xsl:variable name="c" select="sum(/doc/list/movie/rating/@value)" /> 
    <xsl:variable name="d" select="$c div count($a)" /> 
    <xsl:variable name="e" select="count($a) div count($b)" /> 
    <xsl:variable name="f" select="/doc/list/movie/seen[@value ='No']" /> 
    <xsl:variable name="g" select="/doc/list/movie/seen[@value ='Prior']" /> 
    <xsl:variable name="h" select="count($f) div count($b)" /> 
    <xsl:variable name="j" select="count($g) div count($b)" /> 
    <xsl:variable name="minutes_total" select="sum(/doc/list/movie/length[@value ='y'])" /> 
    <xsl:variable name="minutes" select="$minutes_total mod 60" /> 
    <xsl:variable name="hours" select="floor($minutes_total div 60) mod 24" /> 
    <xsl:variable name="hours2" select="floor($minutes_total div 60)" /> 
    <xsl:variable name="days" select="floor($hours2 div 24)" /> 
    <!--End Variables--> 

    <xsl:decimal-format name="percent" /> 
    <xsl:decimal-format name="average" decimal-separator="." /> 

    <xsl:template match="/doc"> 
    <html> 
    <head> 
    <style> 
     h2{ 
     font-family: Courier, Courier New, monospace; 
     font-size: 32px; 
     text-decoration: underline; 
     } 
     body{ 
     font-family: Courier New, monospace; 
     } 
     p{ 
     font-size: 16px; 
     } 
     table{ 
     font-size: 14px; 
     } 
     .title{ 
     text-align:left; 
     } 
     .release{ 
     text-align:center; 
     } 
     .seen{ 
     text-align:center; 
     } 
     .rating{ 
     text-align:right; 
     } 
     .length{ 
     text-align:center; 
     } 
    </style> 
    </head> 
    <body> 
    <h2>My Movie List</h2> 
    <p>Movies seen so far: <xsl:value-of select="count($a)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($e, '#%', 'percent')" /><br /> 
    Movies yet to see: <xsl:value-of select="count($f)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($h, '#%', 'percent')" /><br /> 
    Movies seen prior to making list: <xsl:value-of select="count($g)" />/<xsl:value-of select="count($b)" /> = <xsl:value-of select="format-number($j, '#%', 'percent')" /><br /> 
    Total time watched: <xsl:value-of select="format-number($days, '#0')" /> days, <xsl:value-of select="format-number($hours, '#0')" /> hours, <xsl:value-of select="format-number($minutes, '#0')" /> minutes<br /> 
    Average rating: <xsl:value-of select="format-number($d, '#.000', 'average')" /> stars out of 5</p> 
    <br /> 
    <table border="1"> 
     <tr> 
     <th>#</th> 
     <th>Title</th> 
     <th>Release Date</th> 
     <th>Length</th> 
     <th>Seen</th> 
     <th>Rating</th> 
     </tr> 
     <xsl:for-each select="list/movie"> 
     <xsl:choose> 
     <xsl:when test='seen = "Yes"'> 
     <tr style="background-color:#666; color:#fff"> 
      <td> <xsl:number /></td> 
      <td class="title"><xsl:value-of select="title"/></td> 
      <td class="release"><xsl:value-of select="release"/></td> 
      <td class="length"><xsl:value-of select="length" /> minutes</td> 
      <td class="seen"><xsl:value-of select="seen"/></td> 
      <td class="rating"><xsl:value-of select="rating"/></td> 
     </tr> 
     </xsl:when> 
     <xsl:when test='seen = "Seen prior to making list"'> 
     <tr style="background-color:#999; color:#000"> 
      <td> <xsl:number /></td> 
      <td class="title"><xsl:value-of select="title"/></td> 
      <td class="release"><xsl:value-of select="release"/></td> 
      <td class="length"><xsl:value-of select="length"/> minutes</td> 
      <td class="seen"><xsl:value-of select="seen"/></td> 
      <td class="rating"><xsl:value-of select="rating"/></td> 
     </tr> 
     </xsl:when> 
     <xsl:otherwise> 
     <tr style="background-color:#fff;"> 
      <td> <xsl:number /></td> 
      <td class="title"><xsl:value-of select="title"/></td> 
      <td class="release"><xsl:value-of select="release"/></td> 
      <td class="length"><xsl:value-of select="length" /> minutes</td> 
      <td class="seen"><xsl:value-of select="seen"/></td> 
      <td class="rating"><xsl:value-of select="rating"/></td> 
     </tr> 
     </xsl:otherwise> 
     </xsl:choose> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

<!--Start XML--> 
<list> 
    <movie> 
    <title>2001: A Space Odyssey</title> 
    <release>1968</release> 
    <seen value="No">No</seen> 
    <rating>N/A</rating> 
    <length value="n">141</length> 
    </movie> 
    <movie> 
    <title>28 Days Later</title> 
    <release>2002</release> 
    <seen value="No">No</seen> 
    <rating>N/A</rating> 
    <length value="n">113</length> 
    </movie> 
    <movie> 
    <title>28 Weeks Later</title> 
    <release>2007</release> 
    <seen value="No">No</seen> 
    <rating>N/A</rating> 
    <length value="n">100</length> 
    </movie> 
    <movie> 
    <title>A Clockwork Orange</title> 
    <release>1971</release> 
    <seen value="Yes">Yes</seen> 
    <rating value="2">&#9734;&#9734;&#9734;&#9733;&#9733;</rating> 
    <length value="y">136</length> 
    </movie> 
    <!--Rest of XML--> 
</list> 
</doc> 

Tạo đầu ra mong muốn như hình dưới đây khi lưu lại dưới dạng một tập tin XML và mở trong trình duyệt web FireFox. Định dạng không được hiển thị ở đây, nhưng sẽ là khi bạn mở tệp XML chứa mã trên trong FireFox.

My Movie List 

Movies seen so far: 1/4 = 25% 
Movies yet to see: 3/4 = 75% 
Movies seen prior to making list: 0/4 = 0% 
Total time watched: 0 days, 2 hours, 16 minutes 
Average rating: 2.000 stars out of 5 

# Title   Release Date Length  Seen Rating 
1 2001: A Space Odyssey 1968 141 minutes No  N/A 
2 28 Days Later   2002 113 minutes No  N/A 
3 28 Weeks Later  2007 100 minutes No  N/A 
4 A Clockwork Orange 1971 136 minutes Yes  ☆☆☆★★ 

Lưu ý 1: Vấn đề là sự thay đổi của mức độ yếu tố làm tổ gây ra bằng cách nhúng XML<list>bên trong phần tử<doc>khi bạn đặt XML và XSL sang định dạng tập tin duy nhất. Giải pháp cho biết thêm /doc trước /list trong tất cả các biểu thức XPath của bạn để chọn các yếu tố mong muốn mà hiện nay được lồng một mức độ sâu hơn như hậu duệ của <doc><list> thay vì chỉ con cháu của <list> (như họ khi hai tập tin được tách rời).

Note 2: Loại xml-stylesheet cũng được đổi thành "text/xsl" như một bước tiến trong khả năng tương thích với IE 8. Với "text/xml" loại, IE 8 chỉ hiển thị cây tài liệu (bao gồm XSL) như XML, trong khi với "text/xsl" nó cố gắng - mặc dù khá không thành công - để áp dụng phép biến đổi. Nhiều công việc hơn sẽ được yêu cầu để làm cho màn hình này hiển thị chính xác trong IE 8. Lưu ý rằng giá trị loại nào hoạt động tốt trong FireFox 7. Không có trình duyệt nào khác được kiểm tra.

+0

Cảm ơn bạn rất nhiều! Tôi đã hoàn toàn bị mất với vấn đề này, nhưng giải pháp của bạn đã giúp rất nhiều. Cảm ơn bạn, Witman! – ishmael624

+0

@SamuelPearseWalters: Bạn được chào đón; mừng vì tôi có thể giúp! – Witman

+2

Cập nhật nhanh: Giải pháp hoạt động tốt trên Chrome 29 và Firefox 23. Không hoạt động trên IE10 (trong trường hợp ai cũng muốn biết) –