467,915 Members | 1,110 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

XslTransform Problems with Whitespace

I'm using XslTransform to apply a transform to an XML document, however I
get validation problems when parsing the resulting XML document due to
invalid whitespace. I'm passing in an XPathNavigator for the input to the
Transform method and specifying a Stream for the output.

Here is an example of a transform and a document (it's the IconStyle
sections that are causing problems):

<?xml version="1.0" encoding="UTF-16"?>
<!-- Copyright i2 Ltd 2002-2003. All rights reserved. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-16"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Calls">
<Doc>
<ItemCollection>
<xsl:for-each select="Call">
<xsl:element name="Item">
<xsl:attribute name="Label"><xsl:value-of
select="@from"/></xsl:attribute>
<Icon>
<xsl:element name="IconStyle">
<xsl:attribute name="Type">Telephone</xsl:attribute>
</xsl:element>
</Icon>
</xsl:element>
<xsl:element name="Item">
<xsl:attribute name="Label"><xsl:value-of
select="@to"/></xsl:attribute>
<Icon>
<xsl:element name="IconStyle">
<xsl:attribute name="Type">Telephone</xsl:attribute>
</xsl:element>
</Icon>
</xsl:element>
</xsl:for-each>
</ItemCollection>
</Doc>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-16"?>
<Calls>
<Call from="01339 842744" to="01226 441998" duration="2"/>
<Call from="01339 842744" to="01226 441998" duration="3"/>
<Call from="01663 254564" to="01882 123456" duration="3"/>
<Call from="01226 441998" to="01339 842744" duration="12"/>
<Call from="01882 123456" to="01663 868943" duration="7"/>
</Calls>

I was previously using MSXML4, which would produce something as follows:

<IconStyle Type="Telephone"/>

Now I understand that the above is considered to be identical to the
following:

<IconStyle Type="Telephone"></IconStyle>

Unfortunately when running the same transform on the same document using
XslTransform, the result is as follows:

<IconStyle Type="Telephone">
</IconStyle>

AFAICT this is not equivalent to the MSXML4 result. If I turn off indenting
in the transform then the transformed XML is valid but has no formatting
whatsoever. Am I missing something about using XslTransform or was the
previous behaviour of MSXML4 wrong?

--
Kevin Westhead
Nov 12 '05 #1
  • viewed: 1660
Share:
5 Replies
Kevin Westhead wrote:
I'm using XslTransform to apply a transform to an XML document, however I
get validation problems when parsing the resulting XML document due to
invalid whitespace. I'm passing in an XPathNavigator for the input to the
Transform method and specifying a Stream for the output.

Here is an example of a transform and a document (it's the IconStyle
sections that are causing problems):

<?xml version="1.0" encoding="UTF-16"?>
<!-- Copyright i2 Ltd 2002-2003. All rights reserved. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes" encoding="UTF-16"/>


Turn output indentation off if you care about whitespace in the result
document:
<xsl:output method="xml" indent="no" encoding="UTF-16"/>

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #2
"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:Oc**************@TK2MSFTNGP10.phx.gbl...

<snip>
Turn output indentation off if you care about whitespace in the result
document:
<xsl:output method="xml" indent="no" encoding="UTF-16"/>

Thanks for the response. This is pretty much the behaviour I have observed,
as mentioned at the end of my posting. Can you shed any more light on why
this is the case, e.g. is it expected behaviour based on the W3C specs or is
it just a limitation in XslTransform? Are there any other workarounds that
you know of, e.g. would using a different Transform overload make any
difference?

--
Kevin Westhead
Nov 12 '05 #3
Hi Kevin,

This is a "by design" feature of XSLElment in .NET System.XML.XSL. And this
is different from MSXML 4.0.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 12 '05 #4
"MSFT" <lu******@online.microsoft.com> wrote in message
news:k$**************@cpmsftngxa07.phx.gbl...
This is a "by design" feature of XSLElment in .NET System.XML.XSL. And this is different from MSXML 4.0.


Thank you for responding.

Are there any workarounds? Is it possible for me to disable indenting even
if the loaded stylesheet has indent="yes" specified? I have no control over
the stylesheets, I simply need to apply them to a specified document in
code. I think it would be an extreme measure to say that all stylesheets
cannot specify indenting. Surely this is a bug since the resulting XML is
wrong, i.e. the element contains whitespace when it should be empty? Are you
saying that the following is an acceptable output for the section of the
transform I posted that creates an IconStyle element?

<IconStyle Type="Telephone">
</IconStyle>

If so, is there anyway I can force the output to be either of the following?

<IconStyle Type="Telephone"></IconStyle>

or

<IconStyle Type="Telephone"/>
--
Kevin Westhead
Nov 12 '05 #5
Kevin Westhead wrote:
Are there any workarounds? Is it possible for me to disable indenting even
if the loaded stylesheet has indent="yes" specified? I have no control over
the stylesheets, I simply need to apply them to a specified document in
code.
Sure you can. Just transform to XmlWriter. Then indentation specified in
the stylesheet is ignored and you can disable indentation on that
XmlWriter using Formatting property.
Are you
saying that the following is an acceptable output for the section of the
transform I posted that creates an IconStyle element?

<IconStyle Type="Telephone">
</IconStyle>

Btw, have you tried to ignore such insignificant whitespace while
validating document? Take a look at WhitespaceHandling property of
XmlTextReader class.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Anthony Bouch | last post: by
1 post views Thread by Rodger McNab | last post: by
1 post views Thread by manlio | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.