tags:

views:

32

answers:

2

I've got this incoming XML I'm transforming (with XSLT in ASP using msxsm6):

<Cell>  
  <Data xmlns="http://www.w3.org/TR/REC-html40"&gt;  
    <Font>Text1</Font>  
    <Font> </Font>  
    <Font>Text2</Font>  
  <Data>  
</Cell>

If the template for <Font> is:

<xsl:template match="Font">
  <xsl:copy/>
</xsl:template>

The transform seems to kill off the space character in the 2nd element in the source, the output XML emitted is below, the 2nd element becomes an empty one with no content:

    <Font>Text1</Font>  
    <Font/>  
    <Font>Text2</Font>  

I trialed-and-errored on <xsl:preserve-space elements="Font"/>' but that didn't seem to help. Ideas? Thanks Stackoverflow!

+1  A: 

First, your stylesheet fragment sample is wrong. You would need a rule like this:

<xsl:template match="html:data//node()|html:data//*/@*"
              xmlns:html="http://www.w3.org/TR/REC-html40"&gt; 
  <xsl:copy>
     <xsl:apply-templates select="node()|@*"> 
  </xsl:copy> 
</xsl:template>

Second, about input white space only text nodes. Those would be preserve depending on the XML tree provider. MSXSL doesn't preserve it by default. The only solution is to add xml:space="preserve" attribute in the input source.

Alejandro
+1  A: 

If the template for is:

<xsl:template match="Font"> 
  <xsl:copy/> 
</xsl:template>

The transform seems to kill off the space character in the 2nd element in the source

You are mistaking <xsl:copy> for <xsl:copy-of>

The former only copies athe current element and its namespace nodes (doesn/t copy attributes or descendent nodes), while the latter copies the complete sub-tree rooted in the current node.

Also, you are having namespace problems, as noted by @Alejandro, and it is not possible that the provided XSLT code, when applied on the provided XML document would produce the provided "result".

Dimitre Novatchev
Thanks Dimitre and Alejandro! You're right the output doesn't match the input because, I over-simplified out some of the namespace stuff in the example. Your ideas are right-on target, great help and thanks! Unfortunately I can't alter the incoming XML.
Dave
@Dave: You must provide a true example that demos your problem -- then we will help you find a complete solution. So, edit your question and provide exact information.
Dimitre Novatchev