Groups | Blog | Home
all groups > dotnet xml > april 2004 >

dotnet xml : XSLT Transform options


Paul Hatcher
4/6/2004 9:25:30 PM
Hi

Is it possible to control the node style that XslTransform uses to output
XML?

I'm trying to convert a file from XML to RDF and if I do the transform in
XmlSpy I get the following

<mySchema rdf:resource='fred' />

but if I use the XslTransform class I get

<mySchema rdf:resource='fred'></mySchema>

Unfortunately, my RDF parser doesn't like the second form, so I want to know
if it's possible to tell XslTransform to use the shortened notation?

TIA

Paul

Derek Harmon
4/6/2004 9:50:09 PM
[quoted text, click to view]

Not directly on the XslTransform class, but there is a way (see below).

: :
[quoted text, click to view]

This is a bug in the RDF parser. The Infoset represented by both
serializations is identical.

The reason XSLT doesn't address this is that the particulars of the
XML syntax are irrelevant to the document's content (i.e., it doesn't
matter whether double-quote or single-quote is used to delimit the
attribute values, or empty tags are rendered one way or another).

: :
[quoted text, click to view]

An XmlTextWriter can be wrapped around the output from
XslTransform's Transform( ) method. This XmlTextWriter
can intercept calls to the WriteFullEndElement( ) method,
which XslTransform uses, and replace them with calls to
WriteEndElement( ). WriteEndElement( ) will produce the
shortened end tag when the element has no content.

Here is the code,

- - - XmlTextWriterEE.cs (complete)
using System;
using System.IO;
using System.Xml;

/// <summary>
/// Wrapper that forces shortened empty element end tags to
/// be rendered when possible.
/// </summary>
public class XmlTextWriterEE : XmlTextWriter
{
public XmlTextWriterEE( TextWriter sink) : base( sink) {;}
public override void WriteFullEndElement() {
base.WriteEndElement();
}
}
- - -

The way this might work with an XslTransform, named transformerObj,
would be to wrap some TextWriter (in this example, I wrap the Console
output stream, i.e., stdout).

transformerObj.Transform( xmlDoc.CreateNavigator,
null, new XmlTextWriterEE( Console.Out) );

The XmlTextWriterEE silently intercepts calls that instruct it to write
a full end tag (for instance, to be more compatible with HTML) and
redirects them to the method that'll write out a shortened end tag
for empty elements.


Derek Harmon

NaraendiraKumar R. R.
4/6/2004 11:30:59 PM
Are you using <xsl:output method="xml" version="1.0"/> in your XSLT?

-Naraen

----
[quoted text, click to view]

Paul Hatcher
4/7/2004 7:23:01 AM
Derek

Thanks for that - it was my understanding also that the two forms were
semantically indentical, unfortunately I can't control which RDF parser we
use and I'm under a deadline.:-(

Paul

[quoted text, click to view]

AddThis Social Bookmark Button