I'm learning how to use the XSL transform functionality and can't get
it to work. In a book I'm reading on it, it says that I can do like I
did below and just add a value like <root_node/> in there and I should
be able to transform any XML source document I have so that the
results would simply contain <root_node/>.
I wanted to try that, just so I could see if I understand how this is
working and wrote the code and XSL file below. However, my value for
szTransformedXML is always an empty string. Does anyone see what I'm
doing wrong?
Here's a method I wrote to use it:
private void TransformData(string szXML, out string szTransformedXML)
{
XslTransform oXsl = new XslTransform();
XmlDocument oXMLDoc = new XmlDocument();
oXsl.Load(@"C:\\Srclib\\Test.xsl");
oXMLDoc.LoadXml(szXML);
XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
oXMLDoc = null;
szTransformedXML = oRead.ReadOuterXml();
}
Here's what my Test.xsl file looks like:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<root_node/>
</xsl:template>
</xsl:stylesheet
Sonu Kapoor - 25 Jun 2004 01:17 GMT
>-----Original Message-----
>I'm learning how to use the XSL transform functionality and can't get
[quoted text clipped - 35 lines]
></xsl:stylesheet>
>.
You should use <xsl:value-of select="root_node"/>
See my example:
http://weblogs.asp.net/sonukapoor/articles/162473.aspx
Sonu Kapoor
My Xml HowTo's:
http://weblogs.asp.net/sonukapoor/articles/159790.aspx
Derek Harmon - 25 Jun 2004 02:10 GMT
> However, my value for szTransformedXML is always an empty string.
> Does anyone see what I'm doing wrong?
: :
> XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
> szTransformedXML = oRead.ReadOuterXml();
Your stylesheet is fine; it's how you're using the XmlReader that the
Transform() method is returning to you.
Initially, an XmlReader is in ReadState.Initial. You have to call Read()
on it to cause it to become interactive (i.e., "wake up"). Usually, when
you see an XmlReader used in .NET the code resembles an Iterator
pattern:
while ( oXmlReader.Read( ) )
{
// Do something with Current node.
}
Obviously, this doesn't "throw out" the first node ... it's that the XmlReader
assumes it's one step before the beginning of the XML document. When
you call Read( ), the XmlReader steps up and is positioned on node #0.
The solution then, is to put a call to oRead.Read( ) prior to ReadOuterXml();
although for greater clarity, I'd recommend:
XmlReader oRead = oXsl.Transform(oXMLDoc.CreateNavigator(),null);
oRead.MoveToContent();
szTransformedXML = oRead.ReadOuterXml();
The effect is the same whether you call Read() once or MoveToContent(),
I prefer MoveToContent() when I'm not in a looping situation because it
makes it entirely evident that my intention is to position the XmlReader
onto some content that interests me.
Derek Harmon