html - XSLT 和嵌套元素

标签 html xslt xhtml

鉴于下面的 XSL 模板和 XML,这是我试图实现的 HTML 输出(或多或少):

<p>
foo goes here
  <span class="content"><p>blah blah blah</p><p>blah blah blah</p></span>
bar goes here
  <span class="content">blah blah blah blah blah blah</span> 
</p>

以下是实际渲染的内容( 的全部内容丢失):

<p>
foo goes here
  <span class="content"></span>
bar goes here
  <span class="content">blah blah blah blah blah blah</span> 
</p>

这是我的模板(片段):

 <xsl:template match="note[@type='editorial']">
   <span class="content">
     <xsl:apply-templates />
   </span>
 </xsl>
 <xsl:template match="p">
   <p>
     <xsl:apply-templates />
   </p>
 </xsl>

这是我的 xml:

<p>
foo goes here
  <note type="editorial"><p>blah blah blah</p><p>blah blah blah</p></note>
bar goes here
  <note type="editorial">blah blah blah blah blah blah</note> 
</p>

渲染特定元素并不重要。即。我不关心

是否被渲染,只要没有丢失任何文本元素即可。 我想避免创建特定规则来匹配“p/note/p”,假设 元素可以包含任意子元素。

我对 xsl 完全是个菜鸟,所以任何其他提示或指示都会非常有帮助。

提前致谢。

最佳答案

您应该使用apply-templates而不是apply-template:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="note[@type='editorial']">
        <span class="content">
            <xsl:apply-templates/>
        </span>
    </xsl:template>
    <xsl:template match="p">
        <p>
            <xsl:apply-templates />
        </p>
    </xsl:template>
 </xsl:stylesheet>

关于html - XSLT 和嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13636504/

相关文章:

html - margin 底部不起作用

html - Css 文本不会在网站上加载背景 url 图像?

xml - 如何从我的 xml 文件中删除 BOM 字符

css - 如何设置竖线位置?

html - 有没有办法让 Rails 辅助函数不生成 XHTML 而生成 HTML?

php - 特殊字符替换为?

javascript - 如何仅考虑可见元素来获取当前元素的索引?

xml - XSLT 结合多种功能

xslt - 如何使用 XSLT 将节点集放入属性值中?

html - IE 对象标签备用内容未显示