2010-03-25 67 views
2

首先,這裏是情況。我正在使用一個公會託管站點,該站點允許您將URL輸入到XSL文件和另一個XML輸入。當你想要的所有XML都包含在一個文件中時,一切順利。製作XSL樣式表使用分頁XML工作

我的問題是這樣的Game Roster XML這是分頁...看看接近該文件的底部,你會發現一個<page_links>節,其中包含寫在HTML鏈接到/xml?page=2等尋呼機由於公會託管網站設置只處理一個XML頁面,我無法訪問其他XML頁面。

所以,我只能想到兩種解決方案,但我不知道如何開始

  1. 成立,結合了所有的XML頁面轉換成一個PHP頁面,然後輸出文件。然後,我可以在公會託管站點XSL處理器中使用此URL。
  2. 以某種方式組合XSL樣式表中的所有XML文件。我在SO上找到了this question(我並不是很瞭解它,因爲我不知道document($pXml1)在做什麼),但我認爲它不會起作用,因爲頁數會變化。我認爲這可能是通過加載下一頁,直到<members_to>值等於<members_total>

還有其他想法嗎?我不知道XSL或PHP,所以任何幫助與代碼示例將不勝感激。


更新:我試着上面的方法2,這裏是我遇到麻煩的XSLT片段。代碼的第一頁顯示沒有問題,但我遇到了這個xsl:if,或者可能是document()聲明的問題。

更新#2:將文檔更改爲使用字符串& concat函數,但它仍然不起作用。

<xsl:template name="morepages"> 

<xsl:param name="page">1</xsl:param> 
<xsl:param name="url"> 
    <xsl:value-of select="concat(SuperGroup/profule_url,'/xml?page=')"/> 
</xsl:param> 

<xsl:if test="document(string(concat($url,$page)))/SuperGroup/members_to &lt; document(string(concat($url,$page)))/SuperGroup/members_total"> 
    <xsl:for-each select="document(string(concat($url,$page + 1)))/SuperGroup/members/members_node"> 
    <xsl:call-template name="addrow" /> 
    </xsl:for-each> 
    <!-- Increment page index--> 
    <xsl:call-template name="morepages"> 
    <xsl:with-param name="page" select="$page + 1"/> 
    </xsl:call-template> 
</xsl:if> 
</xsl:template> 

更新#3:感謝您的偉大的開始拉克蘭!但我試圖用HTML輸出。我有一個「addrow」模板和一個主模板,我不知道如何將它與您提供的XSL整合在一起。以下是我有:

<!-- add a member row --> 
<xsl:template name="addrow"> 
    <tr> 
    <td class="coName"> 
    <xsl:element name="a"> 
    <xsl:attribute name="target"> 
     <xsl:text>_blank</xsl:text> 
    </xsl:attribute> 
    <xsl:attribute name="href"> 
     <xsl:value-of select="profile_url"/> 
    </xsl:attribute> 
    <xsl:value-of select="pcname"/> 
    </xsl:element> 
    </td> 
    <xsl:element name="td"> 
    <xsl:attribute name="class"> 
    <xsl:text>coAccount</xsl:text> 
    </xsl:attribute> 
    <xsl:value-of select="pcaccount"/> 
    </xsl:element> 
    <td class="coLevel"><xsl:value-of select="ilevel"/></td> 
    <xsl:if test="irank!=''"> 
    <td class="coRank"> 
    <xsl:value-of select="irank"/> 
    </td> 
    </xsl:if> 
    <td class="coStatus"><xsl:value-of select="pcmapname"/></td> 
    </tr> 
</xsl:template> 


<!-- main template --> 
<xsl:template match="/"> 

<div class="coGuildName"> 
<xsl:element name="img"> 
    <xsl:attribute name="src"> 
    <xsl:text>http://champions-online.com</xsl:text> 
    <xsl:value-of select="SuperGroup/guild_name_img"/> 
    </xsl:attribute> 
</xsl:element> 
</div> 

<table width="100%" cellspacing="0" cellpadding="0" id="coRoster" align="center"> 
<thead> 
    <tr class="ForumCategoryHeader"> 
    <th class="coName">Name</th> 
    <th class="coAccount">Account</th> 
    <th class="coLevel">Level</th> 

    <xsl:if test="SuperGroup/ranks!=''"> 
    <th class="coRank">Rank</th> 
    </xsl:if> 

    <th class="coStatus">Status</th> 
    </tr> 
</thead> 
<tbody> 

<xsl:for-each select="SuperGroup/members/members_node"> 
    <xsl:call-template name="addrow" /> 
</xsl:for-each> 

<!-- if less then total members shown, load next XML page --> 
<xsl:if test="SuperGroup/members_to &lt; SuperGroup/members_total"> 
    <xsl:call-template name="morepages"/> 
</xsl:if> 

</tbody> 
</table> 
</xsl:template> 

更新#4:我仍然堅持。這個XSL不會加載任何額外的XML頁面。我曾嘗試將SuperGroup[position()]添加到SuperGroupmembers模板中,但未成功。我也嘗試將代碼重新格式化爲類似this post的東西,但仍然沒有運氣。我會很感激任何其他的想法。

+0

我敢肯定你可以用XSLT遞歸地做到這一點。該例中的'document'函數調用'pxml1'變量中指定的uri。 – 2010-03-25 20:49:50

+0

@Jweede:謝謝!這是我正在嘗試的方法,但是我在上面的代碼片段中遇到了問題。 – Mottie 2010-03-25 23:08:04

回答

4

此樣式表將頁面「0」作爲輸入文檔。在members模板中,我們檢查更多頁面,並根據需要應用「下一頁」SuperGroup模板。最後一個模板計算頁碼,並從下一個文檔中提取members元素。

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

    <xsl:output method="xml" indent="yes"/> 

    <xsl:variable name="server"> 
     <xsl:text>http://champions-online.com/super_groups/Phoenix%20Force/xml</xsl:text> 
    </xsl:variable> 

    <xsl:template match="SuperGroup"> 
     <members> 
      <xsl:apply-templates select="members"/> 
     </members> 
    </xsl:template> 

    <xsl:template match="members"> 
     <xsl:copy-of select="members_node" /> 
     <xsl:if test="/SuperGroup/members_total != /SuperGroup/members_to"> 
      <xsl:apply-templates select="/SuperGroup" mode="next-page" /> 
     </xsl:if> 
    </xsl:template> 

    <xsl:template match="SuperGroup" mode="next-page"> 
     <xsl:variable name="this" select="(members_from - 1) div 20" /> 
     <xsl:variable name="page" select="1 + $this" /> 
     <xsl:variable name="url" select="concat($server,'?page=',$page)" /> 

     <xsl:apply-templates select="document($url)/SuperGroup/members" /> 
    </xsl:template> 

</xsl:stylesheet> 

將這與您在示例3中的主樣式表合併,我們得到以下內容。

我做了一些普遍的改進:使用文字結果元素而不是xsl:元素,因爲名稱不是動態的;使用屬性值模板而不是xsl:屬性。

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

    <xsl:output method="html"/> 

    <xsl:variable name="server"> 
     <xsl:text>http://champions-online.com/super_groups/Phoenix%20Force/xml</xsl:text> 
    </xsl:variable> 

    <xsl:template match="members_node"> 
     <tr> 
      <td class="coName"> 
       <a target="_blank" href="{profile_url}"> 
        <xsl:value-of select="pcname"/> 
       </a> 
      </td> 
      <td class="coAccount"> 
       <xsl:value-of select="pcaccount"/> 
      </td> 
      <td class="coLevel"> 
       <xsl:value-of select="ilevel"/> 
      </td> 
      <xsl:if test="irank!=''"> 
       <td class="coRank"> 
        <xsl:value-of select="irank"/> 
       </td> 
      </xsl:if> 
      <td class="coStatus"> 
       <xsl:value-of select="pcmapname"/> 
      </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="/"> 
     <div class="coGuildName"> 
      <img src="http://champions-online.com{SuperGroup/guild_name_img}"/> 
     </div> 
     <table width="100%" cellspacing="0" cellpadding="0" id="coRoster" align="center"> 
      <thead> 
       <tr class="ForumCategoryHeader"> 
        <th class="coName">Name</th> 
        <th class="coAccount">Account</th> 
        <th class="coLevel">Level</th> 
        <xsl:if test="SuperGroup/ranks!=''"> 
         <th class="coRank">Rank</th> 
        </xsl:if> 
        <th class="coStatus">Status</th> 
       </tr> 
      </thead> 
      <tbody> 
       <xsl:apply-templates select="SuperGroup/members" /> 
      </tbody> 
     </table> 
    </xsl:template> 

    <xsl:template match="members"> 
     <xsl:apply-templates select="members_node" /> 
     <xsl:if test="/SuperGroup/members_total != /SuperGroup/members_to"> 
      <xsl:apply-templates select="/SuperGroup" mode="next-page" /> 
     </xsl:if> 
    </xsl:template> 

    <xsl:template match="SuperGroup" mode="next-page"> 
     <xsl:variable name="this" select="(members_from - 1) div 20" /> 
     <xsl:variable name="page" select="1 + $this" /> 
     <xsl:variable name="url" select="concat($server,'?page=',$page)" /> 
     <xsl:apply-templates select="document($url)/SuperGroup/members" /> 
    </xsl:template> 

</xsl:stylesheet> 
+0

哇,謝謝你!它比我從其他例子中拼湊出來的要簡單得多。我已經更新了關於如何將XSL與我已經完成的工作集成的問題。正如你在我的代碼中看到的那樣,我正在調用我在第二次更新中包含的「morepages」模板。感謝你目前的幫助! – Mottie 2010-03-26 06:04:46

+0

再次感謝!我不得不將''中的選擇改爲「SuperGroup/members/members_node」以獲得正確的輸出,但它仍然只顯示第一頁。最初的頁面實際上是頁面「1」,但我不確定這是否是問題。 – Mottie 2010-03-26 07:16:37

+0

糟糕,沒關係你是對的,它確實從第0頁開始。 – Mottie 2010-03-26 07:26:40