如何在xslt中从目录中获取所有文件

本文关键字:获取 文件 xslt | 更新日期: 2023-09-27 17:49:19

我需要将多个xml文件合并到一个文件中。我通过xsl转换实现了这一点。xslt文件是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl"
                xmlns:Utils="Utils:Helper">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="assemblies">
    <xsl:copy>
      <xsl:apply-templates select="*"/>
      <xsl:apply-templates select="document('AdminService.xml')/reflection/assemblies/*" />
      <xsl:apply-templates select="document('Helpers.xml')/reflection/assemblies/*" />
      <!--<xsl:value-of select="Utils:GetFiles()"/>-->
    </xsl:copy>
  </xsl:template>
  <xsl:template match="apis">
    <xsl:copy>
      <xsl:apply-templates select="*"/>
      <xsl:apply-templates select="document('AdminService.xml')/reflection/apis/*" />
      <xsl:apply-templates select="document('Helpers.xml')/reflection/assemblies/*" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
我的c#函数是
var xslt = new XslCompiledTransform();
            xslt.Load("XmlMerger.xslt", new XsltSettings { EnableDocumentFunction = true, EnableScript = true}, null);

using (var writer = File.CreateText("XmlDocs''result.xml"))
 {
                xslt.Transform(@"EmployeeService.xml", arguments, writer);
 }

在这三个xml文件EmployeeService, AdminService, Helpers被合并到一个results.xml文件中。这对我来说很好。

现在xml文件的调用是静态的
<xsl:apply-templates select="document('AdminService.xml')/reflection/assemblies/*" />
 <xsl:apply-templates select="document('Helpers.xml')/reflection/assemblies/*" />.

我需要在一个目录中包含所有xml文件。目前,我尝试通过传递字符串(如

)在此xslt文件中调用c#函数。
 public string GetFiles()
        {
            return "<xsl:apply-templates select='"document(''Helpers.xml'')/reflection/assemblies/*'" />";
            //return "Helpers.xml";
        }

注意:只是举个例子,我只包含一个文件。这里我试图构建字符串将其传递到xslt文件

<xsl:value-of select="Utils:GetFiles()"/>

但是在结果中它以纯文本的形式出现。如何转义并告诉它是一个模板,或者如何动态地包含目录中的所有文件?

如何在xslt中从目录中获取所有文件

如果你想用XslCompiledTransform在XSLT中处理XML文档,那么你需要从你的c#代码中传入一个XPathNavigator,如果你想处理XML文档目录,那么你需要传入一个XPathNavigator对象数组。所以你可以写一个方法

    public XPathNavigator[] GetDocuments(string directory)
    {
        return Directory.EnumerateFiles(directory, "*.xml").Select(file => new XPathDocument(file).CreateNavigator()).ToArray();
    }

在示例类MyHelperClass中,在c#代码中实例化该类并将其作为扩展传递给Transform调用:

        XslCompiledTransform xsltProc = new XslCompiledTransform();
        xsltProc.Load("XSLTFile1.xslt");
        XsltArgumentList xsltArgs = new XsltArgumentList();
        xsltArgs.AddExtensionObject("http://example.com/mf", new MyHelperClass());
        xsltProc.Transform("input.xml", xsltArgs, Console.Out);

然后在XSLT中使用例如

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="msxsl mf"
>

,然后你可以处理例如

<xsl:apply-templates select="mf:GetDocuments('someDirectoryName')/root/foo/bar"/>

处理目录中XML文档中的所有bar元素。