xslt transformation and input paths

D

Dan

I have a C# program which executes some XSLT transformations. The XSLT code
requires an input intermediate file, generated by other transformations, as
its duty is copying some data from an input XML while inserting into it
another XML fragment. E.g. let's say this is the original XML:

<Dummy>
<contacts>
... contacts go here ...
</contacts>
</Dummy>

We have then a fragment named ~new.xml:

<header>
<title>That's a dummy sample</title>
</header>

The corresponding XSLT should insert the <header> contents into the original
XML file, e.g.:

<xsl:template match="Dummy">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:copy-of select="document('~new.xml')//header"/>
<xsl:copy-of select="contacts"/>
</xsl:copy>
</xsl:template>

The XSLT above assumes that there is a file named ~new.xml in its path. But
my XSLT does NOT reside in a path as a file, it's stored as a resource in my
application assembly. Now **my question is**: how can the XslTransform
object know where to locate the ~new.xml file fragment, as it is loaded from
an assembly and the location of the original XML varies so that it is known
only at runtime? Is there any way to give it a default input path, or any
other solution to let the code work?

Here's the snippet which loads and executes the XSLT: it works fine, but it
fails to locate the ~new.xml fragment...:

XslTransform xslt = new XslTransform();
XmlUrlResolver res = new XmlUrlResolver();
res.Credentials = System.Net.CredentialCache.DefaultCredentials;
Assembly a = Assembly.GetExecutingAssembly();
Stream st =
a.GetManifestResourceStream("MyNamespace.xslt.MyTransform.xslt");
xslt.Load(new XmlTextReader(new StreamReader(st, Encoding.UTF8)), null,
null);
xslt.Transform(theSourceFile, theTargetFile, res); //<< how to tell it
where to locate ~new.xml?

Thx guys!
 
M

MSFT

Hi Dan,

You may the second XML file "~new.xml" in the same folder as your original
XML file, or specify the URI in the XSL file's Document function.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top