Reputation: 2486
I am trying to insert some XML nodes (plus child nodes) into an XSLT transformed document. I have the XML as a string, and so at first I passed this string in and simply printed it. However, all the <
s and >
s were escaped as <
and >
and so I realized that an XML parser would ignore these and not recognize the string as XML nodes.
I tried first writing the string to a file, and then loading it up using the document() function. However, this resulted in the error: Resolving of external URIs was prohibited.
I also realized that due to the sensitive nature of the documents being transformed this could pose a serious risk, as anybody could write a file with whatever they liked to that location and it would potentially be picked up and inserted into the output XML.
So, I tried passing the XML in as a URI with data:text/xml
appended to it, as the document function is supposed to be able to parse and load URIs of this kind. However, this has resulted in the same error: Resolving of external URIs was prohibited.
My XslCompiledTransform
object is being instantiated correctly, as far as I know. XsltSettings are set to allow the document() function to be used, and I have passed in the XmlUrlResolver object.
var xslt = new XslCompiledTransform(true);
var resolver = new XmlUrlResolver();
#if DEBUG
xslt.Load(Location, new XsltSettings(true, true), resolver);
#else
xslt.Load(XmlReader.Create(new StringReader(this.Text)), new XsltSettings(true, true), resolver);
#endif
I am at somewhat of a loss here. Really, the ideal would be to have a way to pass in the string and tell the Transformation Processor not to escape the <
s and >
s. Is there a way to do this?
Upvotes: 2
Views: 1272
Reputation: 167471
Use e.g.
XPathDocument doc = new XPathDocument(new StringReader(stringWithXml));
XsltArgumentList xsltArgs = new XsltArgumentList();
xsltArgs.AddParameter("param1", "", doc);
...
xslt.Transform(input, xsltArgs, output);
with C# and then <xsl:param name="param1" select="/.."/>
in the XSLT to declare the parameter and initialize it as an empty node set, then you can use <xsl:copy-of select="$param1"/>
to copy the XML to the result where you want to insert it.
Instead of an XPathDocument
you could also use an XmlDocument
or XDocument
I think (might need to explicitly call CreateNavigator()
and the latter).
Upvotes: 4