2009-06-17 78 views
2

我試圖在給定點插入一些HTML。 XML文件有一個內容節點,裏面有實際的HTML。對於〔實施例這裏是XML的內容部分:XSLT插入html內容

----------------- 
<content> 
    <h2>Header</h2> 
    <p><a href="...">some link</a></p> 
    <p><a href="...">some link1</a></p> 
    <p><a href="...">some link2</a></p> 
</content> 
----------------- 

我需要插入一個鏈接頭之後的第一個鏈接之前,自己的p標籤內。用XSLT生鏽一點,任何幫助表示讚賞!

回答

3

鑑於這種來源:

<html> 
    <head/> 
    <body> 
     <content> 
      <h2>Header</h2> 
      <p><a href="...">some link</a></p> 
      <p><a href="...">some link1</a></p> 
      <p><a href="...">some link2</a></p> 
     </content> 
    </body> 
</html> 

該樣式會做你想做的事:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="/html/body/content/h2"> 
     <xsl:copy> 
      <xsl:apply-templates/> 
     </xsl:copy> 
     <p><a href="...">your new link</a></p> 
    </xsl:template> 
</xsl:stylesheet> 
+0

兩個幫助,但它很高興看到放在一起這樣有助於整個事情我瞭解更多。很好的答案謝謝! – Wade 2009-06-17 15:46:33

3
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/content"> 
     <xsl:copy-of select="h2"/> 
     <a href="">foo</a> 
     <xsl:copy-of select="p"/> 
    </xsl:template> 
</xsl:stylesheet>