【问题标题】:Transforming XML to HTML将 XML 转换为 HTML
【发布时间】:2014-01-21 06:38:49
【问题描述】:

我正在使用 XSLT 将 XML 转换为 HTML。

我有以下 XML 结构:

<Info><Description>
<p><strong><span style="font-family: Verdana; color: #f0b444;">Description<br />
<span class="BookingResultTxtBlue">description goes here.</span></span></strong></p>

<p><strong><span style="font-family: Verdana; color: #f0b444;"><span class="BookingResultTxtBlue">Test 1</span></span></strong></p>

<p><strong><span style="font-family: Verdana; color: #f0b444;"><span class="BookingResultTxtBlue">Test 2</span></span></strong></p>

<p><strong><span style="font-family: Verdana; color: #f0b444;"><span class="BookingResultTxtBlue">Test 3</span></span></strong></p>

</Description></Info>

我的 XSLT 如下

<table>
 <tr>
      <td>
       <xsl:value-of  select='Info/Description'/>
     </td>
</tr>
</table>

转换后的html是

<table>
 <tr> 
   <td>description goes here
       Test1
       Test2
       Test3
   </td>
 </tr>
</table>

我想要的是,转换后应用原始 XML 中的样式。

【问题讨论】:

    标签: html asp.net xml xslt


    【解决方案1】:

    你想要的是下面的东西

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Info/Description">
            <table>
                <tr>
                    <td Class="BookingResultTxtBlue">
                        <xsl:apply-templates/>
                    </td>
                </tr>
            </table>
        </xsl:template>
    
        <xsl:template match="Info">
            <xsl:apply-templates/>
        </xsl:template>
    </xsl:stylesheet>
    

    第一个模板复制所有节点,第二个模板将转换为您想要的输出,第三个模板消除了Info 节点。

    【讨论】:

    • 我现在可以正确转换它了。但是我在使用模板时遇到了一个新问题。每当有 &nbsp;在描述中,它显示为  有什么办法可以处理吗?
    • 尝试看看 Mads Hansen (stackoverflow.com/questions/21272212/…) 的这个解决方案
    【解决方案2】:

    代替

    <xsl:value-of  select='Info/Description'/>
    

    尝试:

    <xsl:copy-of select="Info/Description/node()"/>
    

    【讨论】:

      猜你喜欢
      • 2012-05-15
      • 2016-12-30
      • 2014-06-20
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      相关资源
      最近更新 更多