2013-03-15 28 views
1

我想通過使用ColdFusion在XSLT文件中通過URL傳遞兩個參數。使用ColdFusion在URL中傳遞兩個參數的XSLT

這是我的XSLT代碼:

<xsl:template match="/"> 
    <xsl:text>Actors: </xsl:text> 
    <xsl:apply-templates select="/movies/movie/actors/actor/name"/> 
</xsl:template> 

<xsl:template match="name"> 
     <xsl:element name="a"> 
      <xsl:attribute name="href">actor_details.cfm?movieID=<xsl:value-of select="../../../@movieID"/>&amp;actorID=<xsl:value-of select="../@actorID"/></xsl:attribute> 
      <xsl:value-of select="." /> 
     </xsl:element> 
     <xsl:element name="br" /> 
</xsl:template> 

這是我actor_details.cfm文件

<cfset MyXmlFile = Expandpath("test.xml")> 
<cffile action="READ" variable="xmlInput" file="#MyXmlFile#"> 
<cfset MyXslFile = Expandpath("actor_details.xsl")> 
<cffile action="READ" variable="xslInput" file="#MyXslFile#"> 

<cfset xslParam = StructNew() > 
<cfset xslParam["movieID"] = "#url.movieID#" > 

<cfset xmlOutput = XMLTransform(xmlInput, xslInput, xslParam)> 
<!--- data is output ---> 
<cfcontent type="text/html" reset="yes"> 
<cfoutput>#xmloutput#</cfoutput> 

這是基於這樣的我actor_details.xsl文件

<xsl:param name="movieID"/> 

<xsl:template match="/"> 
    <title>Actor details</title> 
    <xsl:apply-templates select="/movies/movie[@movieID=$movieID]/actors/actor[@actorID=$actorID]"/> 
</xsl:template> 

<xsl:template match="actor"> 
    <xsl:text>Name: </xsl:text> 
    <xsl:value-of select="name"/> 
    <xsl:element name="br"/> 
    <xsl:text>Age: </xsl:text> 
    <xsl:value-of select="age"/> 
    <xsl:element name="br"/> 
</xsl:template> 

movieID和actorID通過URL傳遞,actor_details頁面應該顯示演員的姓名和年齡。我對ColdFusion非常陌生,我無法弄清楚如何接收ColdFusion通過URL傳遞的參數。我在actor_details.cfm頁面上收到意外的錯誤。

我想問題在於actor_details.cfm頁面的某處,但我無法弄清楚它是什麼。

我的XML文件:

<movie movieID="1"> 
    <actors> 
     <actor actorID="1"> 
      <name>Bob</name> 
      <age>23</age> 
     </actor> 

     <actor actorID="2"> 
      <name>Jack</name> 
      <age>25</age> 
     </actor> 

     <actor actorID="3"> 
      <name>James</name> 
      <age>38</age> 
     </actor> 
    </actors> 
</movie> 

<movie movieID="2"> 
    <actors> 
     <actor actorID="1"> 
      <name>Mike</name> 
      <age>19</age> 
     </actor> 

     <actor actorID="2"> 
      <name>Daniel</name> 
      <age>29</age> 
     </actor> 

     <actor actorID="3"> 
      <name>Phil</name> 
      <age>41</age> 
     </actor> 
    </actors> 
</movie> 
+0

在您的操作頁面上執行。 – 2013-03-16 00:43:40

+0

好吧,我已經完成了,它爲第一個演員顯示「結構actorID 1,MovieID 1」。如果我點擊其他名稱,它會根據actorID和movieID正確顯示結構。所以它似乎工作,但我仍然在下面意想不到的錯誤。我不知道爲什麼。 – Alex 2013-03-16 00:51:09

+0

另外,如果您沒有穩健的調試輸出和日誌記錄,請在您的開發服務器的管理員中打開它,它將對您有很大的幫助。確保選擇應用程序,會話,CGI範圍輸出。它會減慢你的頁面,但你只能打開你的IP和開發服務器。 – 2013-03-16 01:16:31

回答

0

我已經通過將<cfset xslParam["actorID"] = URL.actorID >添加到我的CFM文件中並將<xsl:param name="actorID"/>添加到我的XSL文件中來解決了該問題。

+0

修改刪除不必要的#號 – Leigh 2013-03-16 16:32:11

0

要獲得關於URL變量的問題。網址是CF的可變範圍之一,可以像訪問:

<cfset myvar = #URL.actorid# > 

當然,你不必使用右邊的#跡象,但因爲你剛開始學習它確定一個好辦法你CF變量。

這裏是一個鏈接到CF 9文件和CF的內置variable scopes

+0

因此,我可以將 myvar =#URL.movi​​eID#>?由於我使用的是xslParam,我不太清楚如何使用這個代碼而不是原來的代碼 – Alex 2013-03-16 01:31:18

+1

@Alex你仍然想使用''' ##'沒有必要。這個答案沒有多大幫助。 – 2013-03-16 03:20:32

+0

@Alex - 最好從右腳開始。不要用那種方式使用#號。他們完全沒有必要。雖然它不會導致語法錯誤,但通常認爲它編碼不佳,甚至Adobe也會[阻止使用無關的#號](http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7fc3。 HTML)。如果你是CF的新手,你可能會發現這個鏈接很有用:[由英鎊符號決定](http://www.coldfusionmuse。com/index.cfm/2011/2/10/when.do.i.use.pound.signs) – Leigh 2013-03-16 03:20:43