我想編程插入一個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>
謝謝!
不幸的是,那不是這麼做的。我認爲問題可能是這個'$(「div#append_here」)'。無論是在你的版本還是我的版本中,jquery都沒有成功定位到外部的xsl文檔。 – NateH06
我確實在周圍發現了一個黑客。不幸的是,它不涉及任何'append'方法。在原始的xsl表中,我有這個: ' < xsl:apply-templates>'然後我簡單地在我的html頁面中使用它: $(stylesheet).find(「xsl \\:sort,sort」)。first()。attr(「select」, sortByElement);'where sortByElement ='model,year,price等 –
NateH06