首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用XSLT 2.0和xsl的Camel :result-document将文件保存在路由目的地中

使用XSLT 2.0和xsl的Camel :result-document将文件保存在路由目的地中
EN

Stack Overflow用户
提问于 2019-03-15 00:52:53
回答 1查看 656关注 0票数 2

我正在尝试使用XSLT2.0转换来根据项目组将XML文件拆分成更小的文件。为了执行转换,我使用了一个驼峰路由。我的问题是,当运行xslt转换时,生成的文件被保存在“路由”之外。

因此,例如,给定以下驼峰路由:

from("{{input.endpoint}}")
.to("xslt:xslts/ics/MappingMapTostudents.xslt?saxon=true")
.to("{{output.endpoint}}");

我期望在{output.endpoint}文件夹中创建生成的xml文件。不幸的是,saxon将文件保存在可执行文件(camel应用程序)所在的根文件夹中。如何将文件保存到{{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>
EN

回答 1

Stack Overflow用户

发布于 2019-03-15 04:24:27

我对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 Camel的人可能会提出一个更优雅的解决方案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55168069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档