【问题标题】:XSLT and nested elementsXSLT 和嵌套元素
【发布时间】:2012-11-29 23:12:16
【问题描述】:

鉴于下面的 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>

这是我的模板 (sn-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 的菜鸟,所以任何额外的提示或指针都会很有帮助。

提前致谢。

【问题讨论】:

  • 您没有在 XSL 尝试匹配的输入 XML 中显示 &lt;note ...&gt; 标记。请编辑您的帖子并在输入中的正确位置提供&lt;note&gt; 包装标签。
  • xml输入中有两个元素,最后一个块引用。顶部的两个块是输出。

标签: html xslt xhtml


【解决方案1】:

您应该使用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>

【讨论】:

  • 谢谢,我修正了原文中的错字,但仍然没有解决我的问题。
【解决方案2】:

好的,所以我只是大惊小怪,这是我最终想出的解决方案。

嵌套的

标签不起作用。您的浏览器不喜欢它们,XSLT 也不喜欢它们。所以,我把所有东西都换成了

另外,我在模板末尾添加了几个包罗万象的模板。

这是对我的目的来说运行良好的最终版本:

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

<xsl:template match="p">
  <div class="para">
    <xsl:apply-templates />
  </div>
</xsl:template>

<xsl:template match="*">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select="." />
</xsl:template>

小时/吨:

http://www.dpawson.co.uk/xsl/sect2/defaultrule.html

How can xsl:apply-templates match only templates I have defined?

【讨论】:

    【解决方案3】:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
        <xsl:apply-templates select="p"/>
        </xsl:template>
        <xsl:template match="p">
        <p>
        <xsl:apply-templates/>
        </p>
        </xsl:template>
        <xsl:template match="span">
         <note type="editorial">
         <xsl:choose>
         <xsl:when test="child::*">
         <xsl:copy-of select="child::*"/>
         </xsl:when>
         <xsl:otherwise>
                    <xsl:value-of select="."/>
        </xsl:otherwise>
        </xsl:choose>
        </note>
        </xsl:template>
        <xsl:template match="text()">
        <xsl:copy-of select="."/>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

    • 这没有达到预期的输出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2012-06-28
    • 1970-01-01
    相关资源
    最近更新 更多