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 / March 2007

Tip: Looking for answers? Try searching our database.

how to apply xslt within xml to that using .NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RccH - 20 Mar 2007 12:57 GMT
Beginner using xslt... So. I have an XML file which has a link into
xslt file like following...

<?xml version='1.0' encoding='utf-8' ?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<customers> rest of the xml....

So How, using C# or VB.NET do I apply that xsl file into this same
xmlfile and write the transformed file into disk?
Any help would be appreciated...
Martin Honnen - 20 Mar 2007 14:21 GMT
> Beginner using xslt... So. I have an XML file which has a link into
> xslt file like following...
[quoted text clipped - 5 lines]
> So How, using C# or VB.NET do I apply that xsl file into this same
> xmlfile and write the transformed file into disk?

That <?xml-stylesheet ...?> thing is a processing instruction so you can
read the whole XML file into an XPathDocument, then navigate to that
processing instruction and read out the target data, a regular
expression might help for that.

            XPathDocument xmlDoc = new XPathDocument(@"XMLFile1.xml");
            XPathNavigator navigator = xmlDoc.CreateNavigator();
            if (navigator.MoveToChild(XPathNodeType.ProcessingInstruction))
            {
                if (navigator.Name == "xml-stylesheet")
                {
                    string target = navigator.Value;
                    Match match = Regex.Match(target,
@"href=""([^""]+)""");
                    if (match.Success)
                    {
                        string href = match.Groups[1].Value;
                        navigator.MoveToRoot();
                        XslCompiledTransform xsltProcessor = new
XslCompiledTransform();
                        xsltProcessor.Load(href);
                        using (XmlWriter xmlWriter =
XmlWriter.Create(Console.Out, xsltProcessor.OutputSettings))
                        {
                            xsltProcessor.Transform(xmlDoc, xmlWriter);
                        }
                    }
                }
            }

In reality it might be more complex as the href URI is relative to the
XML document so you need to make sure you resolve it correctly.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

RccH - 21 Mar 2007 10:28 GMT
> > Beginner using xslt... So. I have an XML file which has a link into
> > xslt file like following...
[quoted text clipped - 43 lines]
>         Martin Honnen --- MVP XML
>        http://JavaScript.FAQTs.com/

Thank you. That helped... especially the part using regular expression
for href parsing...

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.