我正在尝试在给定点插入一些 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 有点生疏,感谢任何帮助!
最佳答案
鉴于此来源:
<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>
关于html - XSLT 插入 html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1007700/