开发者

Transforming XSLT text element with "one space character" yields empty content

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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜