1
A
回答
3
你開始
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
然後添加模板轉換的節點模板要變換例如
<xsl:template match="td">
<th>
<xsl:apply-templates select="@* | node()"/>
</th>
<xsl:template>
變換所有td
元素注入th
元件或
<xsl:template match="h6"/>
刪除所有h6
元素。
如果你想改變一個td
那麼你需要一些方法來識別它,假設它有一個id
屬性使用匹配模式像
<xsl:template match="td[@id = 'td1']">
<td style="background-color: lightgreen;">
<xsl:apply-templates select="@* | node()"/>
</td>
</xsl:template>
這樣設置特定td
的CSS background-color
財產元件。
0
這個任務最好最根本的XSLT設計模式進行處理:使用和重寫identity rule:
讓我們有這個XML文檔:
<root>
<x>This is:</x>
<a>
<b>
<c>hello</c>
</b>
</a>
<a>
<b>
<c1>world</c1>
</b>
</a>
<a>
<b>!</b>
</a>
<y>The End</y>
</root>
任務是:
全部刪除
c
個元素。將任何
b
元素重命名爲d
。在任何
c1
元素後插入c2
元素。
的解決方案是令人驚訝的簡短:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="c"/>
<xsl:template match="b">
<d><xsl:apply-templates select="node()|@*"/></d>
</xsl:template>
<xsl:template match="c1">
<xsl:call-template name="identity"/>
<c2>xxx</c2>
</xsl:template>
</xsl:stylesheet>
當該變換是在上面的XML文檔應用,有用的,正確的結果產生:
<root>
<x>This is:</x>
<a>
<d/>
</a>
<a>
<d>
<c1>world</c1>
<c2>xxx</c2>
</d>
</a>
<a>
<d>!</d>
</a>
<y>The End</y>
</root>
說明:
當選擇執行或按名稱調用時,標識模板將「按原樣」複製當前節點。
對於每一個特殊的任務(刪除,插入和重命名,我們添加符合要求的節點,從而覆蓋了身份模板一個單獨的模板,因爲較高的特異性。
壓倒一切的,更具體的模板選擇了它相匹配,並且在節點上執行performes,我們需要執行該任務。
相關問題
- 1. 用html標籤替換bbcode
- 2. 如何替換Html標籤?
- 3. 動態替換html標籤
- 4. 用html替換php標籤
- 5. 替換HTML標籤內容
- 6. 用jquery替換html標籤
- 7. HTML標籤可以繼承父/封閉標籤的類嗎?
- 8. 原則繼承替換
- 9. XSLT用生成的標籤替換標籤
- 10. Excel:用換行符替換html標籤
- 11. Yii繼承屬性標籤
- 12. 搜索「繼承」標籤
- 13. 使用XSLT將標籤換成標籤
- 14. PHP:如何將html標籤替換爲「」?
- 15. Javascript html替換標籤不工作
- 16. 正則表達式替換HTML標籤
- 17. PHP的字符串替換HTML標籤
- 18. 替換文本忽略HTML標籤
- 19. 用bb代碼替換html標籤
- 20. 使用的preg_replace來替換HTML標籤
- 21. 用RegEx替換HTML標籤內容
- 22. 用HTML標籤替換符號
- 23. greasemonkey幫助(替換html標籤)
- 24. 匹配模式和HTML標籤替換
- 25. 替換html標籤中的單詞
- 26. JSON文件替換HTML標籤
- 27. 如何替換HTML標籤的文本
- 28. HTML標籤是替換元素嗎?
- 29. 使用sed替換HTML標籤內容
- 30. lxml:clean_html用div替換html標籤?
需要一些更多的信息 –
是的,有如何做到這一點的SO和其他很多很多的例子。該要查找的關鍵概念是_identity template_ - 低優先級模板e只是將其輸入複製到輸出不變,然後您可以使用更具體的模板覆蓋任何要處理的內容。 –
代碼示例?我可以標記單個標記並在子xslt文檔中覆蓋它嗎?或者我必須重複一個結構?伊恩,任何鏈接? – Sergejs