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>