在xslt之后保留HTML标记 [英] retain HTML tags after xslt

查看:87
本文介绍了在xslt之后保留HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xslt解析树.

i have following xslt parsing tree.

<xsl:template match="div[@class='tr-summaryinfo']"> 
    <ul>
  <xsl:apply-templates/>
  </ul>
  </xsl:template>
 <xsl:template match="p[@class='tr-summaryitem']">
   <li>
 <xsl:apply-templates/> 
  </li>
 </xsl:template>
 <xsl:template match="body">
   <div id="storybodycontent">
      <span class="storycontent">
       <xsl:copy-of select="node()"/>
     </span>
   </div>
  </xsl:template>

输入为:

<html test="http://www.w3.org/1999/xhtml">
<head>
<title>US STOCKS-PepsiCo, oil help extend Wall St rally; S&amp;P at 4-month high</title>
</head>
<body>
<div class="tr-summaryinfo">
<p class="tr-summaryitem">Energy shares jump as oil gains on supply disruptions </p>
<p class="tr-summaryitem">PepsiCo's best day in 7 yrs on strong Q2</p>
<p class="tr-summaryitem">Nordstrom down on disappointing forecast</p>
<p class="tr-summaryitem">S&amp;P 500 Q2 earnings growth seen at 21 pct -TR I/B/E/S</p>
<p class="tr-summaryitem">Tesla gains on plans to open new plant in Shanghai</p>
<p class="tr-summaryitem">Indexes up: Dow 0.56 pct, S&amp;P 0.30 pct, Nasdaq 0.14 pct</p>
</div>
<p class="tr-advisory">Changes comment, adds details, updates prices</p><p class="tr-by">By Amy Caren Daniel</p><p class="tr-story-p1"><span class="tr-dateline">July 10 (Reuters)</span><span class="tr-dl-sep"> - </span>

我想在解析后保留一些标签. BUt我当前的解析返回纯文本.

i want to retain some tags as it is after parsing. BUt my current parsing returns plain text.

<div class="tr-summaryinfo"><p class="tr-summaryitem">将按预期进行转换,其余内容将松散以纯文本形式返回的标签.

<div class="tr-summaryinfo"> and <p class="tr-summaryitem"> being converted as expected, rest of the content looses its tags returned as plain text.

<xsl:copy-of select="node()"/>会按原样返回所有标签,但会呕吐转换.

<xsl:copy-of select="node()"/> returns all tags as they are, but vomits transformation.

请帮助.**

推荐答案

由于您需要保留输出中的某些标记,因此可以从identity transform template开始,该输入将输入原样复制到输出中. >

Since you need to retain some tags as is in the output, you can start with an identity transform template which copies the input as is to the output.

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>

在匹配body的模板中,使用<xsl:apply-templates>而不是<xsl:copy-of>.

In the template matching body use <xsl:apply-templates> instead of <xsl:copy-of>.

<xsl:template match="body">
    <div id="storybodycontent">
        <span class="storycontent">
            <xsl:apply-templates />
        </span>
    </div>
</xsl:template>

完整的XSLT看起来像

The complete XSLT will look like

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:strip-space elements="*" />

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="body">
        <div id="storybodycontent">
            <span class="storycontent">
                <xsl:apply-templates />
            </span>
        </div>
    </xsl:template>

    <xsl:template match="div[@class='tr-summaryinfo']">
        <ul>
            <xsl:apply-templates />
        </ul>
    </xsl:template>

    <xsl:template match="p[@class='tr-summaryitem']">
        <li>
            <xsl:apply-templates />
        </li>
    </xsl:template>
</xsl:stylesheet>

这应该提供所需的输出.

This should give the required output.

这篇关于在xslt之后保留HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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