C# 如何获得XSL文件目录的绝对路径?

C# 如何获得XSL文件目录的绝对路径?,c#,xml,xslt,xsd,filepath,C#,Xml,Xslt,Xsd,Filepath,我的Schema.xsd文件与.xsl文件位于同一目录中。在.xsl文件中,我想在生成的输出中生成指向Schema.xsl的链接。生成的输出位于不同的目录中。目前我是这样做的: <xsl:template match="/"> <root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../

我的Schema.xsd文件与.xsl文件位于同一目录中。在.xsl文件中,我想在生成的输出中生成指向Schema.xsl的链接。生成的输出位于不同的目录中。目前我是这样做的:

   <xsl:template match="/">
     <root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="../../../Schema.xsd">
     <!-- . . . -->
...
<xsl:param name="SchemaLocation"/> <!-- this more or less at the top of your XSLT! -->
...

<xsl:template match="/">
   <root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="{$SchemaLocation}">
   ...
   ...
</xsl:template>
....
但是,这会强制生成的输出位于Schema.xsd目录下的3个级别。我想在输出中生成模式的绝对路径,这样输出就可以位于任何位置


更新。我在.NET Framework 4.5中使用XSLT 1.0 XslCompiledTransform实现。

这是如何实现的示意图,请参见

在C代码中,您需要定义并使用参数列表:

XsltArgumentList argsList = new XsltArgumentList();
argsList.AddParam("SchemaLocation","","<SOME_PATH_TO_XSD_FILE>");

XslCompiledTransform transform = new XslCompiledTransform();
transform.Load("<SOME_PATH_TO_XSLT_FILE>");

using (StreamWriter sw = new StreamWriter("<SOME_PATH_TO_OUTPUT_XML>"))
{
    transform.Transform("<SOME_PATH_TO_INPUT_XML>", argsList, sw);
} 
您的XSLT可以这样增强:

   <xsl:template match="/">
     <root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="../../../Schema.xsd">
     <!-- . . . -->
...
<xsl:param name="SchemaLocation"/> <!-- this more or less at the top of your XSLT! -->
...

<xsl:template match="/">
   <root version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="{$SchemaLocation}">
   ...
   ...
</xsl:template>
....
XSLT2.0解决方案 使用XPath 2.0函数:

在不传递参数且不考虑输入XML的情况下产生:


为什么不将XSL文件的目录作为参数传递给XSLT进程?@MarcusRickert,谢谢您的提示。我是XSLT新手。我将不得不研究是否可以在C中使用XslCompiledTransform进行转换,因为我以这种方式运行xsl文件。我使用XSLT1.0,但是这个答案对于在2.0中遇到类似问题的其他人可能有用。或者我可以稍后切换到2.0。