Html 在 XSLT 的 XML 中使用 <br /> 标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14567794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using <br /> tag within XML for XSLT
提问by Cobus
I am currently running into some trouble with an XML/XSLT to HTML conversion I am working on.
In short, I want to use <br />
tags within an XML tag, so that the HTML file after transformation will display a line break. After some tries I got that to work, however at the cost of some other functionality. Namely the ability to highlight parts.
我目前在处理 XML/XSLT 到 HTML 的转换时遇到了一些麻烦。总之,我想<br />
在一个 XML 标签中使用标签,这样转换后的 HTML 文件会显示一个换行符。经过一些尝试,我得到了它的工作,但是以其他一些功能为代价。即突出显示部分的能力。
First the dumped down XML file. So basically, there are several possible tags which all contain a first and last name. In this case I want the first and last name to be parsed on a separate line (hence the <br />
tag). Furthermore in some instances a first or last name will need to be highlighted. In this case on line 3, last name "The Hand".
首先是倾倒的 XML 文件。所以基本上,有几个可能的标签都包含名字和姓氏。在这种情况下,我希望在单独的行上解析名字和姓氏(因此是<br />
标签)。此外,在某些情况下,需要突出显示名字或姓氏。在本例中,在第 3 行,姓氏“The Hand”。
<swift_native>
<tag tag_code=":1:"><![CDATA[Jaco<br />Ronnie]]></tag>
<tag tag_code=":2:"><![CDATA[John<br />Doe]]></tag>
<tag tag_code=":2:"><![CDATA[Robbie<br />]]><highlight>The Hand</highlight></tag>
</swift_native>
So far, depending on the method I use within the XSLT I either am able to get the linebreaks correct or the highlighting. But not both: The following figure shows this.
到目前为止,根据我在 XSLT 中使用的方法,我能够正确地获得换行符或突出显示。但不能两者兼而有之:下图显示了这一点。
Below you see the used XSLT file. Where you can see that using
<xsl:apply-templates/>
will make the highlighting work and <xsl:value-of select="." disable-output-escaping="yes"/>
will let me use the <br />
correctly.
您可以在下方看到使用过的 XSLT 文件。您可以在哪里看到使用
<xsl:apply-templates/>
将使突出显示工作并使<xsl:value-of select="." disable-output-escaping="yes"/>
我<br />
正确使用。
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- HTML Layout definition -->
<xsl:output method="html"/>
<xsl:template match="swift_native">
<html>
<head>
<title>
<xsl:apply-templates select="message_id"/>
</title>
<style type="text/css">
#tbl1,#tbl2 {display:none;}
#lnk1,#lnk2 {border:none;background:none;width:85px;}
td {FONT-SIZE: 75%; MARGIN: 0px; COLOR: #000000;}
td {FONT-FAMILY: verdana,helvetica,arial,sans-serif}
a {TEXT-DECORATION: none;}
table.subtable {border-collapse:collapse;}
table.subtable td {border:1px solid black;}
</style>
</head>
<body>
<table cellpadding="3" width="100%" class="subtable">
<tr bgcolor="#cccccc">
<td colspan="3">Block4</td>
</tr>
<xsl:apply-templates select="tag" />
</table>
</body>
</html>
</xsl:template>
<!-- Variable definition -->
<xsl:template match="tag">
<tr>
<td>
<b>
<xsl:value-of select="@tag_code" />
</b>
</td>
<td>
<xsl:value-of select="." disable-output-escaping="yes"/>
</td>
<td>
<xsl:apply-templates/>
</td>
</tr>
</xsl:template>
<xsl:template match="highlight">
<span style="background-color:yellow;">
<xsl:apply-templates/>
</span>
</xsl:template>
</xsl:stylesheet>
Obviously, the question is: does someone know a way in which I can use both the <br />
tag and the highlighting?
显然,问题是:有人知道我可以同时使用<br />
标签和突出显示的方法吗?
回答by harpo
CDATA
is telling the processor to interpret the contents as plain text, not markup. That's why disable-output-escaping
is needed to prevent the <br/>
from showing up as <br/>
.
CDATA
告诉处理器将内容解释为纯文本,而不是标记。这就是为什么disable-output-escaping
需要防止<br/>
显示为<br/>
.
If you want to take advantage of disable-output-escaping
, you'll have to break up the way that you select against the tag content.
如果您想利用disable-output-escaping
,则必须打破针对标签内容进行选择的方式。
Add a template
添加模板
<xsl:template match="tag/text()">
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>
and change the value-of
line to
并将该value-of
行更改为
<xsl:apply-templates select="text()|*"/>
回答by JLRishe
One solution here is to use both:
这里的一种解决方案是同时使用两者:
<xsl:template match="tag">
<tr>
<td>
<b>
<xsl:value-of select="@tag_code" />
</b>
</td>
<td>
<xsl:apply-templates/>
</td>
</tr>
</xsl:template>
<xsl:template match="tag//text()">
<xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>
<xsl:template match="highlight">
<span style="background-color:yellow;">
<xsl:apply-templates />
</span>
</xsl:template>
Note however that if you do this, you need to ensure that any text values within the <tag>
nodes are properly escaped in the CDATA, and doublyescaped outside it, that is, rather than
但是请注意,如果您这样做,您需要确保<tag>
节点内的任何文本值都在 CDATA 中正确转义,并在其外部双重转义,即,而不是
<tag tag_code=":2:"><![CDATA[Robbie & Bobbie <br />]]><highlight> & The Hand</highlight></tag>
You would need to have:
你需要:
<tag tag_code=":2:"><![CDATA[Robbie & Bobbie<br />]]><highlight> &amp; The Hand</highlight></tag>
So this is probably not a great approach if the <tag>
elements have any possibility of containing XML special characters.
因此,如果<tag>
元素有可能包含 XML 特殊字符,这可能不是一个好方法。
If you can ensure that the text directly below the <tag>
will alwaysbe in a CDATA and that anything in lower nodes (e.g. <highlight>
s) won't, then it's mildly simpler. You can replace the text matching template above with this one:
如果您可以确保直接在 下方的文本<tag>
将始终在 CDATA 中,并且较低节点(例如<highlight>
s)中的任何内容都不会,那么它会稍微简单一些。你可以用这个替换上面的文本匹配模板:
<xsl:template match="tag/text()">
<xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>
And then you just need to ensure that the stuff in the CDATA is properly escaped, and that anything else is just valid XML.
然后您只需要确保 CDATA 中的内容被正确转义,并且其他任何内容都只是有效的 XML。
Finally, if you have some control over your source data, you should consider abandoning the CDATA and just having the <br />
right in the <tag>
:
最后,如果您对源数据有一定的控制权,您应该考虑放弃 CDATA 而只拥有以下<br />
权限<tag>
:
<tag tag_code=":2:">Robbie<br /><highlight>The Hand</highlight></tag>
Then you could use this XSL, which is much more robust than anything that uses disable-output-escaping
:
然后你可以使用这个 XSL,它比任何使用的东西都要健壮得多disable-output-escaping
:
<xsl:template match="tag">
<tr>
<td>
<b>
<xsl:value-of select="@tag_code" />
</b>
</td>
<td>
<xsl:apply-templates/>
</td>
</tr>
</xsl:template>
<xsl:template match="tag/@* | tag/node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="highlight">
<span style="background-color:yellow;">
<xsl:apply-templates />
</span>
</xsl:template>
回答by user508402
Happily, there's a simple solution too. Just add this line to your xsl file:
令人高兴的是,也有一个简单的解决方案。只需将此行添加到您的 xsl 文件中:
<xsl:template match="br"><br/></xsl:template>
This way, there's no need to wrap the data into CDATA, but instead use the much more intuitive:
这样,就不需要将数据包装到 CDATA 中,而是使用更直观的:
<tag tag_code=":1:">Jaco<br/>Ronnie</tag>
Likewise other common simple HTML tags may be included. Here's an example linking bold, italic, etc. to CSS styles, but a one-liner for each (like above) does work too:
同样,可以包括其他常见的简单 HTML 标签。这是一个将粗体、斜体等链接到 CSS 样式的示例,但每个样式的单行(如上)也可以:
<xsl:template match="i|b|u|strong">
<span>
<xsl:attribute name="class">html_<xsl:value-of select="name(.)" /></xsl:attribute>
<xsl:apply-templates />
</span>
</xsl:template>
If you find yourself doing this often, copy them all to html.xsl and use xsl:include
to use them when needed.
如果您发现自己经常这样做,请将它们全部复制到 html.xsl 并xsl:include
在需要时使用它们。
回答by Amirouche Zeggagh
if you want to make in html is
如果你想做 在 html 中是
all wath you need is to make in XLS :
您只需要在 XLS 中制作:
<br></br>