使用< br />用于XSLT的XML标记 [英] Using <br /> tag within XML for XSLT

查看:171
本文介绍了使用< br />用于XSLT的XML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到了一些麻烦,我正在处理一个XML / XLST到HTML转换。
简而言之,我想在XML标签中使用< br /> 标签,以便转换后的HTML文件显示换行符。经过一些尝试后,我得到了这个工作,但是以其他一些功能性为代价。即突出显示部分的能力。

首先是倾倒的XML文件。所以基本上,有几个可能的标签都包含名字和姓氏。在这种情况下,我希望在单行上解析名字和姓氏(因此< br /> 标签)。此外,在某些情况下,需要突出显示姓氏或名字。在这种情况下,在第3行,姓手。

 < swift_native> 
< tag tag_code =:1:><![CDATA [Jaco< br /> Ronnie]]>< / tag>
< tag tag_code =:2:><![CDATA [John< / tag>]>< / tag>
< tag tag_code =:2:><![CDATA [Robbie< br />]]>< highlight>手< / highlight>< / tag>
< / swift_native>

到目前为止,根据我在XLST中使用的方法,我能够获得换行符正确或突出显示。但不是两种:下图显示了这一点。




下面您会看到使用过的XLST文件。在哪里可以看到使用< xsl:apply-templates /> 将使突出显示工作,并且< xsl:value-of select = disable-output-escaping =yes/> 会让我正确使用< br /> 。 b
$ b

 <?xml version =1.0?> 
< xsl:stylesheet version =2.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
<! - HTML布局定义 - >
< 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:折叠;}
table.subtable td {border:1px纯黑色;}
< / style>
< / head>
< body>
< table cellpadding =3width =100%class =subtable>
< tr bgcolor =#cccccc>
< td colspan =3> Block4< / td>
< / tr>
< xsl:apply-templates select =tag/>
< / table>
< / body>
< / html>
< / xsl:template>

<! - 变量定义 - >

< xsl:template match =tag>
< tr>
< td>
< b>
< xsl:value-of select =@ tag_code/>
< / b>
< / td>
< td>
< / 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>

很明显,问题是:有人知道我可以同时使用< br /> 标记作为高亮显示?

解决方案

CDATA 告诉处理器将内容解释为纯文本,而不是标记。这就是为什么需要 disable-output-escaping 来防止< br /> 显示为< br /& gt;



如果你想利用禁用输出转义,你必须打破你选择标签内容的方式。



添加一个模板

p>

 < xsl:template match =tag / text()> 
< / xsl:template>

并将一行更改为

 < xsl:apply-templates select =text()| */> 


I am currently running into some trouble with an XML/XLST 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 functionallity. Namely the abillity to highlight parts.

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 seperate 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".

<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 XLST I either am able to get the linebreaks correct or the highlighting. But not both: The following figure shows this.

Below you see the used XLST 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.

<?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 as the highlighting?

解决方案

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 &lt;br/&gt;.

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.

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

<xsl:apply-templates select="text()|*"/>

这篇关于使用&lt; br /&gt;用于XSLT的XML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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