【问题标题】:unclosed html tag inside xsltxslt内未关闭的html标签
【发布时间】:2012-09-17 23:15:36
【问题描述】:

由于 xslt,我希望有未关闭的 html 标记。稍后我将在 xslt 中添加结束标记。我怎样才能做到这一点?这个不编译:

<xsl:when test="$href">
     <xsl:text><a href='{$href}'></xsl:text>
 </xsl:when>

感谢

【问题讨论】:

    标签: java html xml xslt


    【解决方案1】:

    您可能应该不惜一切代价避免这种事情。我不知道你的要求,但你可能想要一个基于某些东西的链接或跨度标签。

    在这些情况下,您可以使用类似的东西

    <xsl:apply-templates select="tag"/>
    

    然后是 2 个模板,即

    <xsl:template match="tag">
        <span>hello king dave</span>    
    </xsl:template>
    
    <xsl:template match="tag[@href]">
        <a href="{@href}">link text....</a>    
    </xsl:template>
    

    【讨论】:

      【解决方案2】:

      如果没有更好地了解确切的用例,很难给出明确的答案,但值得注意的是,您可以在同一个&lt;xsl:template&gt; 上使用match name。例如,如果您想为所有 &lt;tag&gt; 元素生成一些特定的输出,但在某些情况下还要将此输出包装在 &lt;a&gt; 标记中,那么您可以使用类似的成语

      <xsl:template match="tag[@href]">
        <a href="{@href}"><xsl:call-template name="tagbody" /></a>
      </xsl:template>
      
      <xsl:template match="tag" name="tagbody">
        Tag content was "<xsl:value-of select="."/>"
      </xsl:template>
      

      这里的想法是带有hreftag 元素将匹配第一个模板,该模板在调用通用tag 模板之前和之后进行一些额外的处理。没有href 的标签只会命中没有包装逻辑的普通模板。 IE。对于像

      这样的输入
      <root>
        <tag>foo</tag>
        <tag href="#">bar</tag>
      </root>
      

      你会得到类似的输出

      Tag content was "foo"
      <a href="#">Tag content was "bar"</a>
      

      【讨论】:

        【解决方案3】:

        我之前遇到过同样的问题,只能通过为每个 when 分支复制整个 &lt;a href='{$href}'&gt;...&lt;/a&gt; 来解决它。

        也许您可以尝试将 XSL 的 doctype 设置为一些宽松的 XML 标准,但是 afaik XSLT 非常严格。

        编辑:显然您可以使用&lt;xsl:output&gt; 标签设置文档类型。

        【讨论】:

          【解决方案4】:

          在网上找到解决方法:

                  <xsl:text disable-output-escaping="yes"><![CDATA[<a href=']]></xsl:text>
                           <xsl:value-of select="href"/>
                  <xsl:text disable-output-escaping="yes"><![CDATA['>]]></xsl:text>
          

          【讨论】:

          • 请检查我的答案,使用 disable-output-escaping 只能作为最后的手段!并且经常被误解 XSLT 的人使用,这通常会导致原本简单的解决方案过于复杂:)
          猜你喜欢
          • 2011-03-04
          • 2010-10-03
          • 1970-01-01
          • 2012-01-19
          • 2023-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多