Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / June 2004

Tip: Looking for answers? Try searching our database.

XSL Transform - can't get it to work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Doug - 24 Jun 2004 23:02 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.