2017-03-17 31 views
2

我想編程插入一個appendTo()方法與jQuery。我有一個格式化樣式表,它格式化一個XML表,但我用第三個HTML文檔使用jQuery和JavaScript。我原來的XML的格式如下:應用appendTo添加一個排序節點申請模板

<guitars> 
    <guitar> 
     <model>AStrat</model> 
     <year>1978</year> 
     <name>Strat</name> 
     <price>2500</price> 
    </guitar> 
</guitars> 

和我的樣式表是這樣的:

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

    <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/"> 
     <table id="guitarTable" border="1" width="200"> 
      <tr class="header"> 
       <th>Model</th> 
       <th>Year</th> 
       <th>Name</th> 
       <th>Price</th> 
      </tr> 
      <xsl:apply-templates select="/guitars/guitar"> 
      </xsl:apply-templates> 
     </table> 
    </xsl:template> 

    <xsl:template match="guitar"> 
     <tr> 
      <td> <xsl:value-of select="model" /> </td> 
      <td> <xsl:value-of select="year" /> </td> 
      <td> <xsl:value-of select="name"/> </td> 
      <td> <xsl:value-of select="price" /> </td> 
     </tr> 
    </xsl:template> 

</xsl:stylesheet> 

這裏是我的JavaScript。 stylesheet是上面通過ajax導入的樣式表,並且我檢查了正常工作。

var nodeToAppend = '<xsl:sort select="model" data-type="text"/>' 
$(nodeToAppend).appendTo($(stylesheet) 
           .find("xsl\\:apply-templates, apply-templates") 
           .first()); 

但是,這似乎並不想採取。稍後,我通過XSLT處理器將XSL應用於XML。任何想法我做錯了什麼? 基本上我想這行:

<xsl:apply-templates select="/guitars/guitar"> 
       </xsl:apply-templates> 

成爲:

<xsl:apply-templates select="/guitars/guitar"> 
      <xsl:sort select="model" data-type="text"/> 
</xsl:apply-templates> 

謝謝!

回答

0

XSLT

 <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/> 

     <xsl:template match="/"> 
      <table id="guitarTable" border="1" width="200"> 
       <tr class="header"> 
        <th>Model</th> 
        <th>Year</th> 
        <th>Name</th> 
        <th>Price</th> 
       </tr> 
       <div id="append_here"> 
       <xsl:apply-templates select="/guitars/guitar"> 
       </xsl:apply-templates> 
       </div> 
      </table> 
     </xsl:template> 

     <xsl:template match="guitar"> 
      <tr> 
       <td> <xsl:value-of select="model" /> </td> 
       <td> <xsl:value-of select="year" /> </td> 
       <td> <xsl:value-of select="name"/> </td> 
       <td> <xsl:value-of select="price" /> </td> 
      </tr> 
     </xsl:template> 

    </xsl:stylesheet> 

的jquery

//<xsl:apply-templates><![CDATA[ 
    $(function() { 
    $("div#append_here").children().append('<xsl:sort select="model" data-type="text"/>'); 
    }); 
//]]></xsl:apply-templates> 

JSFIDDLE

+0

不幸的是,那不是這麼做的。我認爲問題可能是這個'$(「div#append_here」)'。無論是在你的版本還是我的版本中,jquery都沒有成功定位到外部的xsl文檔。 – NateH06

+0

我確實在周圍發現了一個黑客。不幸的是,它不涉及任何'append'方法。在原始的xsl表中,我有這個: ' < xsl:apply-templates>'然後我簡單地在我的html頁面中使用它: $(stylesheet).find(「xsl \\:sort,sort」)。first()。attr(「select」, sortByElement);'where sortByElement ='model,year,price等 – NateH06

1

你可以簡單地在XSLT定義一個全局參數<xsl:param name="sort-key"/>,然後有

 <xsl:apply-templates select="/guitars/guitar"> 
      <xsl:sort select="*[local-name() = $sort-key]"/> 
     </xsl:apply-templates> 

然後,而不是操縱在每次需要更改鍵時,XSLT代碼,您只需設置的參數例如不同給出var proc = new XSLTProcessor();與一個導入的樣式表,然後你可以設置例如在運行下一個轉換之前,請使用proc.setParameter('', 'sort-key', 'model');。沒有必要操縱XSLT樣式表。

+0

感謝您的回覆,這將工作。不幸的是,由於我的要求,我不得不通過動態改變XSLT來獲得我的結果。 – NateH06

相關問題