【问题标题】:XSLT Insert html contentXSLT 插入 html 内容
【发布时间】:2010-11-03 17:01:18
【问题描述】:

我正在尝试在给定点插入一些 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 xml xslt


    【解决方案1】:

    鉴于此来源:

    <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>
    

    【讨论】:

    • 两者都有帮助,但是很高兴看到整个事情像这样放在一起帮助我理解了更多。好的答案谢谢!
    【解决方案2】:
    <?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>
    

    【讨论】:

      猜你喜欢
      • 2014-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多