【问题标题】:How to use the XSLT fn:document function in Saxon-HE?如何在 Saxon-HE 中使用 XSLT fn:document 函数?
【发布时间】:2014-10-06 02:02:47
【问题描述】:

如何在 Saxon-HE 中使用 XSLT fn:document 函数?我知道.net 可以使用 Xml Resolver 的“EnableDocumentFunction”。如何/在我下面的编码中插入 XmlResolver。

#region "saxon"
public static StringWriter saxonmethod(string sourceFile, Stream XSLT)
{
    Processor processor = new Processor();

    TextReader stringReader = new StringReader(sourceFile);
    XmlTextReader reader2 = new XmlTextReader(stringReader);
    reader2.XmlResolver = null;// Load the source document
    XdmNode input = processor.NewDocumentBuilder().Build(reader2);


    // Create a transformer for the stylesheet.
    XsltTransformer transformer = processor.NewXsltCompiler().Compile(XSLT).Load();
    transformer.InputXmlResolver = null;

    // Set the root node of the source document to be the initial context node
    transformer.InitialContextNode = input;

    // Create a serializer
    StringWriter s = new StringWriter();
    Serializer serializer = new Serializer();//serializer.SetOutputWriter(Console.Out);
    serializer.SetOutputWriter(s);

    // Transform the source XML to System.out.
    transformer.Run(serializer);
    return s;
}

#endregion

感谢您的提前。萨兰

【问题讨论】:

    标签: c# .net xml xslt saxon


    【解决方案1】:

    尝试简单地删除transformer.InputXmlResolver = null; 行,然后我认为document/doc 应该可以工作。如果没有,那么你可以设置transformer.InputXmlResolver = new XmlUrlResolver();

    【讨论】:

    • 嗨 Martin,我对相关问题还有一个疑问,我的 xslt 使用了“exslt”,所以我得到了 Saxon.Api.DynamicError,“找不到一个匹配的 1-argument 函数,名为 {@987654321 @}node-set()。没有本地名称node-set的Saxon扩展函数,请帮忙解决。
    • @saravanans,这是一个完全不同的问题,你应该问一个新问题。但是使用 XSLT 2.0 就不需要该功能,因此只需替换任何使用例如exsl:node-set($var) 与例如$var.
    • 嗨 Margin,非常感谢您的大力帮助,它对我来说工作正常
    【解决方案2】:

    你没有说你的样式表在做什么,或者它是如何失败的,所以我们在这里工作相当黑暗。我的猜测是您对 document() 调用的参数是一个相对 URI,它失败了,因为没有已知的基本 URI,或者不正确的基本 URI。默认情况下,document() 使用样式表的基本 URI,当样式表从 Stream 加载时未知,因此您需要在 XsltCompiler 上设置 BaseUri 属性。

    【讨论】:

    • 您好,感谢您的快速回复。我的 XML 。我的 Xslt 调用 document() 函数但从不工作,当从 Oxygen 转换时它对我来说工作正常:
    • 好的,您正在调用 document() 并提供一个节点作为参数,在这种情况下,重要的是该节点的基本 URI。因此,您需要设置用于构建源文档的 DocumentBuilder 的 BaseUri 属性。或者,不要费心创建 XmlTextReader,直接从文件中构建(),然后 Saxon 将知道基本 URI 作为该文件的位置。
    • 嗨 Michael Kay,我对相关问题还有一个疑问,我的 xslt 使用了“exslt”,所以我得到了 Saxon.Api.DynamicError,“找不到一个匹配的 1-argument 函数,名为 {@ 987654321@}node-set()。没有本地名称node-set的Saxon扩展函数,请帮忙解决。
    • @saravanans,请尝试更有条理。这看起来是一个完全不相关的问题,所以请将其作为一个新问题提出。解释您想要实现的目标、您编写的代码、您使用的 XSLT 处理器和版本,以及它是如何失败的。
    猜你喜欢
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2019-02-09
    相关资源
    最近更新 更多