Bashnia007
Bashnia007

Reputation: 135

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?

Upvotes: 0

Views: 478

Answers (2)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

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>

Upvotes: 1

michael.hor257k
michael.hor257k

Reputation: 116959

The result that you show us is incomplete. I am guessing you're trying to do something like:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dc="http://schemas.datacontract.org/2004/07/WeekReportJob"
exclude-result-prefixes="dc">
<xsl:strip-space elements="*"/>

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

<xsl:template match="dc:Document">
    <p>
        <xsl:value-of select="dc:p/dc:span" />
    </p>
    <xsl:apply-templates select="dc:table"/>
</xsl:template>

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

</xsl:stylesheet>

Applied to your input example, the result wil be:

<html>
   <body>
      <h1>Boss</h1>
      <p>Table 1</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>
   </body>
</html>

Upvotes: 1

Related Questions