简体   繁体   中英

Transforming XML to HTML

I am transforming XML into HTML using XSLT.

I have the following XML structure:

<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>

My XSLT is as follows

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

After tansformation html is

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

What i want here is, styles in original XML to be applied after transformation.

What you want is something like below

<?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>

The first template copies all nodes, the second template does the transformation to your desired output, and the third template eliminates the Info node.

Instead of

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

try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM