在 .NET XSLT 中使用 document() 函数会产生错误 [英] using document() function in .NET XSLT generates error

查看:20
本文介绍了在 .NET XSLT 中使用 document() 函数会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 XSLT 文件中使用嵌入的资源,但是在调用document(...)"时 C# 抱怨加载文档时出错..."

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

我想在 XSLT 文件中使用定义的资源并通过以下方式获取它们:document('')//my:resources/"...

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

我该怎么做??

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>

我怎样才能在 C# 中毫无例外地访问这样的结构?我将在通过 ex 进行静态转换期间添加它.Opera 一切正常.

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.

推荐答案

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

select 属性的值不是语法正确的 XPath 表达式.每个兼容的 XSLT 处理器都必须引发错误.

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

解决方案:

将以上内容更正为:

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

如果仍然出现异常,请阅读XsltSettings 类.

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

然后使用 的实例="noreferrer">这个构造函数,像这样:

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

XsltSettings(true, false)

不要启用脚本 -- 将构造函数的第二个参数保持为 false.

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

以下是更完整的代码片段:

// 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());

更新:另一个可能的错误原因是 XSLT 样式表是在内存中动态创建的(不是来自文件).在这种情况下,XSLT 处理器通常无法解析 document('') 中的相对 uri.

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

在最后一种情况下,解决方案是使所需元素成为 xsl:variable 的内容,并使用 xxx:node-set() 扩展函数来解决这个元素.

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.

这篇关于在 .NET XSLT 中使用 document() 函数会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆