tags:

views:

150

answers:

2

I'd like to use embedded resources in my XSLT file, but while invoking 'document(...)' C# complains that "Error during loading document ..."

I'd like to use defined resources in XSLT file and get them by this: "document('')//my:resources/"...

How can i do that??

ex xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:my="xslt-gruper-v1.2.xsl" exclude-result-prefixes="my">

     <my:resources>
      <one>tryb</one>
     </my:resources>

     <xsl:variable name="res" select="document('')/*/my:resources/("/>
</xsl:stylesheet>

How can i get access to such structure without exceptions in C#? I'll add that during static transform via ex. Opera everything works fine.

+1  A: 
<xsl:variable name="res" select="document('')/*/my:resources/("/>

The value of the select attribute is not a syntactically correct XPath expression. Every compliant XSLT processor must raise an error.

Solution:

Correct the above to:

<xsl:variable name="vRes" select="document('')/*/my:resources"/>

If there is still an exception raised, do read about the XsltSettings class.

Then create an instance of XsltSettings with this constructor, like this:

XsltSettings(true, false)

Do not enable scripting -- keep the second argument of the constructor as false.

Below is a more complete code snippet:

// Create the XsltSettings object with document() enabled and script disabled.
XsltSettings settings = new XsltSettings(true,false);

// Create the XslCompiledTransform object and load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("sort.xsl", settings, new XmlUrlResolver());

Update: Another possible reason for an error is when the XSLT stylesheet is dynamically created in memory (doesn't come from file). In this case an XSLT processor typically cannot resolve the relative uri in document('').

In this last case the solution is to make the wanted element the content of an xsl:variable and to use the xxx:node-set() extension function to address this element.

Dimitre Novatchev
A: 

Sorry my mistake... I had correct XPath error while copy-paste...

But i've tried your solution and i've still errors. I see now "error while loading document. This operation is not supported with relative URI"...

Any idea?

Simon
Please post minimal but complete samples (XML, XSLT, C#) that allow us to reproduce the problem.
Martin Honnen
This is not "relative uri": `document('')/*/my:resources`.The only case this might fail is when your XSLT stylesheet is generated dynamically (not coming from file). In this last case you cannot use `document('')`.
Dimitre Novatchev
I updated my answer to include describe the last case and to provide a solution for it.
Dimitre Novatchev