如何在 <xsl:attribute> 内部生成标记文本? [英] How to generate markup inside of &lt;xsl:attribute&gt; text?

查看:22
本文介绍了如何在 <xsl:attribute> 内部生成标记文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 XSLT 样式表生成 Bootstrap HTML,其中某些元素可能包含 data-... 属性将附加数据传递给框架.例如,我有这个代码来生成一个 popover 元素:

My XSLT stylesheet generates Bootstrap HTML where some elements may contain data-... attributes to pass additional data to the framework. For example, I have this code to generate a popover element:

<xsl:template match="foo">
  <a href="#" data-toggle="popover" data-placement="top" data-trigger="hover" data-html="true">
    <xsl:attribute name="title">Popover Title</xsl:attribute>
    <xsl:attribute name="data-content">This is some additional content.</xsl:attribute>
    <xsl:text>Link</xsl:text>
  </a>
</xsl:template>

data-content 属性应该包含额外的标记.结果输出应该类似于

The data-content attribute is supposed to contain additional markup. The resulting output should be something like

<a href="#" ... data-content="This is <em>some</em> additional <a href='#'>content</a>.">Link</a>

在这种情况下,如何为 生成标记文本?

How do I generate markup text for the <xsl:attribute> in this case?

(有点相关:此处这里此处.)

答案

感谢您的回答!虽然我认为 kjhughes 的答案 提供了技术上正确的正确实施我需要的解决方案,我认为 伊恩的回答 更直接地解决了我的问题.

Thanks for the answers! While I think that kjhughes's answer provides the technically correct solution to implement properly what I need, I think that Ian's answer addresses my question more directly.

推荐答案

您不能将未转义的标记放在属性值中,但是 您不需要 - 如果您转义尖括号(以及任何与符号和引号内的引号),因为实体引用引导程序仍会在弹出窗口中正确呈现 html.

You can't put unescaped markup in an attribute value, but you don't need to - if you escape the angle brackets (and any ampersands and quotes-within-quotes) as entity references bootstrap will still render the html properly in the popover.

<a href="#" data-content="This is &lt;em&gt;some&lt;/em&gt; additional &lt;a href='#'&gt;content&lt;/a&gt;.">Link</a>

在 XSLT 中实现这一点的最简单方法是使用 CDATA 部分:

The simplest way to get this right in the XSLT would be to use a CDATA section:

<xsl:attribute name="data-content"
  ><![CDATA[This is <em>some</em> additional content
    &amp; a <a href="#">link</a>.]]></xsl:attribute>

序列化程序会根据需要为您转义.

And the serializer will escape it for you as necessary.

这篇关于如何在 <xsl:attribute> 内部生成标记文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆