Bashnia007 Bashnia007 - 3 months ago 18
HTML Question

Save html tags in xslt

[Update]

I found solution!) All that i heed is to use

<xsl:copy-of select="dc:Document"> instead of <xsl:value-of select="dc:Document">
. Thanks everybody)

I need to convert xml-file using xslt. I can convert most part of my xml-file but have a problem when it's nesessary to save html tags during converting. I need to save all formating at tag
<Document>...</Document>
without changes. I think that I have to put some attribut to tag



<?xml version="1.0" encoding="utf-16"?>
<MyItem xmlns="http://schemas.datacontract.org/2004/07/WeekReportJob" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<JobPos>Boss</JobPos>
<Document>
<p>
<span>Table 1</span>
</p>
<table>
<tr>
<td align="left" valign="top">
<p>
<span>Cell 1</span>
</p>
</td>
<td align="left" valign="top" colspan="2">
<p>
<span>Cell 2</span>
</p>
</td>
</tr>
</table>
</Document>
</MyItem>





My XSLT-file is



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://schemas.datacontract.org/2004/07/WeekReportJob"
xmlns:a="http://schemas.datacontract.org/2004/07/DataBaseModel.EF">

<xsl:template match="/">
<html>
<body>
<xsl:value-of select="dc:MyItem/dc:JobPos" />
<xsl:value-of select="dc:MyItem/dc:Document" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>





I want the following result



Boss
<p>
<span>Table 1</span>
</p>
<table>
<tr>
<td align="left" valign="top">
<p>
<span>Cell 1</span>
</p>
</td>
<td align="left" valign="top" colspan="2">
<p>
<span>Cell 2</span>
</p>
</td>
</tr>
</table>





Is it possible?

Answer

You can try the following stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:dc="http://schemas.datacontract.org/2004/07/WeekReportJob" 
    xmlns:a="http://schemas.datacontract.org/2004/07/DataBaseModel.EF" exclude-result-prefixes="dc a">

    <xsl:strip-space elements="*"/>
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:template match="/">
        <html>
            <body>
                <xsl:value-of select="dc:MyItem/dc:JobPos" />
                <xsl:apply-templates select="dc:MyItem/dc:Document/node()" />
            </body>
        </html>
    </xsl:template>

    <!-- remove namespaces -->
    <xsl:template match="dc:*">
        <xsl:element name="{local-name()}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
Comments