我正在尝试使用XSLT 2.0转换将XML文件拆分为基于项目组的较小文件。要执行转换,我正在使用骆驼路线。我的问题是,当运行XSLT转换时,将结果文件保存在"外部"路线上。
因此,给定以下骆驼路线:
from("{{input.endpoint}}")
.to("xslt:xslts/ics/MappingMapTostudents.xslt?saxon=true")
.to("{{output.endpoint}}");
我希望在{output.endpoint}文件夹中创建所得的XML文件。不幸的是,撒克逊人将文件保存在可执行文件(骆驼应用)所在的根文件夹中。如何将文件保存到{{output.endpoint}}或更高,传递给以下端点?
遵循XML示例:
<?xml version="1.0" encoding="UTF-8"?>
<class>
<students>
<student>
<firstname>Albert</firstname>
<group>A</group>
</student>
<student>
<firstname>Isaac</firstname>
<group>A</group>
</student>
<student>
<firstname>Leonardo</firstname>
<group>B</group>
</student>
<student>
<firstname>Enrico</firstname>
<group>B</group>
</student>
<student>
<firstname>Marie</firstname>
<group>C</group>
</student>
<student>
<firstname>Rosalind</firstname>
<group>C</group>
</student>
<student>
<firstname>Ada</firstname>
<group>D</group>
</student>
</students>
</class>
XSLT转换:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:grp="http://www.altova.com/Mapforce/grouping" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="grp xs fn">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:function name="grp:var2_function">
<xsl:param name="var1_param" as="node()"/>
<xsl:for-each select="$var1_param/student">
<xsl:sequence select="fn:string(group)"/>
</xsl:for-each>
</xsl:function>
<xsl:template match="/">
<xsl:for-each-group select="class/students" group-by="grp:var2_function(.)">
<xsl:variable name="var3_resultof_grouping_key" as="xs:string" select="current-grouping-key()"/>
<xsl:result-document href="{fn:concat($var3_resultof_grouping_key, '.xml ')}" encoding="UTF-8">
<class>
<xsl:for-each select="(current-group()/student)[(fn:string(group) = $var3_resultof_grouping_key)]">
<students>
<student>
<firstname>
<xsl:sequence select="fn:string(firstname)"/>
</firstname>
<group>
<xsl:sequence select="$var3_resultof_grouping_key"/>
</group>
</student>
</students>
</xsl:for-each>
</class>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
我对apache-camel一无所知,不幸的是,关于stackoverflow上的技术似乎没有很高的成功率。
http://camel.apache.org/xslt.html上的文档不给出有关如何设置基本输出URI的任何线索,撒克逊人使用saxon用于解决xsl:result-document/@href
中出现的任何相对URI。
最简单的方法是将参数传递给转换,其值是输出目录的绝对路径:
<xsl:param name="outDir" as="xs:string"/>
...
<xsl:result-document
href="file:///{$outDir}{$var3_resultof_grouping_key}.xml"/>
但是一个知道Apache骆驼的人可能会建议一个更优雅的解决方案。
您可以尝试强制类型"文件"的输出。
to("xslt:xslts/ics/MappingMapTostudents.xslt?saxon=true&output=file")
然后,正如文档所解释的那样,您必须用想要的目标文件路径填充骆驼标头:
对于 file 您必须用键指定inter中的文件名 Exchange.xslt_file_name,也是camelxsltfilename。此外,必须事先创建导致文件名的任何路径,否则在运行时抛出异常。
祝你好运