[英]How to get all the files from a directory in xslt
I have a requirement to merge multiple xml files in to a single file. 我需要将多个xml文件合并到一个文件中。 I achieved this by xsl transform.
我通过xsl转换实现了这一目标。 My xslt file is
我的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>
My C# function is 我的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);
}
In this three xml files EmployeeService, AdminService, Helpers are merged into a single file results.xml. 在这三个xml文件中,将EmployeeService,AdminService和Helpers合并到单个文件results.xml中。 This is working fine for me.
这对我来说很好。
Now the calling of the xml files is static 现在,xml文件的调用是静态的
<xsl:apply-templates select="document('AdminService.xml')/reflection/assemblies/*" />
<xsl:apply-templates select="document('Helpers.xml')/reflection/assemblies/*" />.
I need to include all the xml files in a directory. 我需要在目录中包含所有xml文件。 Currently i tried by calling C# function in this xslt file by passing string like
目前我尝试通过传递像这样的字符串在此xslt文件中调用C#函数
public string GetFiles()
{
return "<xsl:apply-templates select=\"document(\'Helpers.xml\')/reflection/assemblies/*\" />";
//return "Helpers.xml";
}
Note: Just for example i included only one file. 注意:例如,我仅包含一个文件。 Here I am trying to build that string pass it to the xslt file
在这里,我正在尝试构建将该字符串传递给xslt文件的字符串
<xsl:value-of select="Utils:GetFiles()"/>
But in the results its coming as plain text. 但结果显示为纯文本。 How to escape this and tell its a template or how to dynamically include all the files from a directory ?
如何对此进行转义并告知其模板,或如何动态包含目录中的所有文件?
If you want to process an XML document in XSLT with XslCompiledTransform
then you need to pass in an XPathNavigator
from your C# code, if you want to process a directory of XML documents then you need to pass in an array of XPathNavigator
objects. 如果要使用
XslCompiledTransform
在XSLT中处理XML文档,则需要从C#代码传递XPathNavigator
,如果要处理XML文档目录,则需要传递XPathNavigator
对象数组。 So you can write a method 所以你可以写一个方法
public XPathNavigator[] GetDocuments(string directory)
{
return Directory.EnumerateFiles(directory, "*.xml").Select(file => new XPathDocument(file).CreateNavigator()).ToArray();
}
in a sample class MyHelperClass
, instantiate that class in your C# code and pass it as an extension to the Transform
call: 在示例类
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);
Then in your XSLT use eg 然后在您的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"
>
and then you can process eg 然后您可以处理例如
<xsl:apply-templates select="mf:GetDocuments('someDirectoryName')/root/foo/bar"/>
to process all bar
elements found in the XML documents in the directory. 处理目录中XML文档中找到的所有
bar
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.