【发布时间】:2012-09-17 23:15:36
【问题描述】:
由于 xslt,我希望有未关闭的 html 标记。稍后我将在 xslt 中添加结束标记。我怎样才能做到这一点?这个不编译:
<xsl:when test="$href">
<xsl:text><a href='{$href}'></xsl:text>
</xsl:when>
感谢
【问题讨论】:
由于 xslt,我希望有未关闭的 html 标记。稍后我将在 xslt 中添加结束标记。我怎样才能做到这一点?这个不编译:
<xsl:when test="$href">
<xsl:text><a href='{$href}'></xsl:text>
</xsl:when>
感谢
【问题讨论】:
您可能应该不惜一切代价避免这种事情。我不知道你的要求,但你可能想要一个基于某些东西的链接或跨度标签。
在这些情况下,您可以使用类似的东西
<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>
【讨论】:
如果没有更好地了解确切的用例,很难给出明确的答案,但值得注意的是,您可以在同一个<xsl:template> 上使用match 和 name。例如,如果您想为所有 <tag> 元素生成一些特定的输出,但在某些情况下还要将此输出包装在 <a> 标记中,那么您可以使用类似的成语
<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>
这里的想法是带有href 的tag 元素将匹配第一个模板,该模板在调用通用tag 模板之前和之后进行一些额外的处理。没有href 的标签只会命中没有包装逻辑的普通模板。 IE。对于像
<root>
<tag>foo</tag>
<tag href="#">bar</tag>
</root>
你会得到类似的输出
Tag content was "foo"
<a href="#">Tag content was "bar"</a>
【讨论】:
我之前遇到过同样的问题,只能通过为每个 when 分支复制整个 <a href='{$href}'>...</a> 来解决它。
也许您可以尝试将 XSL 的 doctype 设置为一些宽松的 XML 标准,但是 afaik XSLT 非常严格。
编辑:显然您可以使用<xsl:output> 标签设置文档类型。
【讨论】:
在网上找到解决方法:
<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>
【讨论】: