2014-10-06 50 views
0

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

#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 

感谢Adavance。 Saran

回答

0

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

+0

非常感谢,它的工作很好。 – 2014-10-06 10:41:08

+0

嗨马丁,我有一个更多的怀疑相关的问题,我的xslt使用“exslt”,以便我得到它Saxon.Api.DynamicError,“无法找到匹配的1个参数函数名为{http://exslt.org/common } node-set()。没有使用本地名称node-set的撒克逊扩展函数,请帮助解决。 – 2014-10-29 03:18:27

+0

@saravanans,这是一个完全不同的问题,你应该问它一个新的问题。但是对于XSLT 2.0,不需要该功能,只需替换任何用途即可。 'exsl:node-set($ var)'例如'$ var'。 – 2014-10-29 08:47:11

0

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

+0

嗨感谢您的快速回复。 我的XML 。我的Xslt调用document()函数,但从来没有工作,它从Oxygen转换时对我很好: 2014-10-06 10:28:01

+0

好的, document()提供一个节点作为参数,在这种情况下,它是该节点的基本URI。因此,您需要设置用于构建源文档的DocumentBuilder的BaseUri属性。或者,不要打扰直接从文件创建XmlTextReader,只需创建(),然后Saxon将知道基础URI作为该文件的位置。 – 2014-10-06 10:50:50

+0

嗨迈克尔凯,我有一个更多的怀疑相关的问题,我的xslt使用“exslt”,以便我得到它Saxon.Api.DynamicError,“找不到匹配的1个参数函数名为{http://exslt.org/ common} node-set()。没有使用本地名称node-set的撒克逊扩展函数,请帮助解决。 – 2014-10-29 03:21:07