Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

c# - using document() function in .NET XSLT generates error

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.

question from:https://stackoverflow.com/questions/65830962/how-to-transform-xml-string-using-xslt-stylesheet-linked-by-xml-stylesheet-href

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...