XSLT插入html内容

时间:2009-06-17 15:16:57

标签: html xml xslt

我正在尝试在给定点插入一些HTML。 XML文件有一个内容节点,里面有实际的HTML。例如,这里是XML的内容部分:

-----------------
<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有点生疏,感谢任何帮助!

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>