all groups > dotnet xml > march 2007 >
You're in the

dotnet xml

group:

how to apply xslt within xml to that using .NET


how to apply xslt within xml to that using .NET RccH
3/20/2007 4:57:04 AM
dotnet xml:
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...
Re: how to apply xslt within xml to that using .NET Martin Honnen
3/20/2007 2:21:26 PM
[quoted text, click to view]

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.


--

Martin Honnen --- MVP XML
Re: how to apply xslt within xml to that using .NET RccH
3/21/2007 2:28:07 AM
[quoted text, click to view]

Thank you. That helped... especially the part using regular expression
for href parsing...
AddThis Social Bookmark Button