簡體   English   中英

如何從xslt中的目錄獲取所有文件

[英]How to get all the files from a directory in xslt

我需要將多個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()"/>

但結果顯示為純文本。 如何對此進行轉義並告知其模板,或如何動態包含目錄中的所有文件?

如果要使用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元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM