2

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 &lt; and &gt; 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?

3
  • 1
    Regarding "Resolving of external URIs was prohibited.", see stackoverflow.com/questions/32439036/…. For the rest, please construct a minimum, self-contained runnable code sample along with the expected output. There are several ways to get new nodes into an XML document, ImportNode() being one of them. Why does it have to be XSLT? Commented Feb 11, 2016 at 14:43
  • @Tomalak I already saw that question. It is partly how I got to the situation I am in now. Commented Feb 11, 2016 at 14:48
  • ...and the other things in my comment? Commented Feb 11, 2016 at 15:01

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.