2017-04-12 48 views
0

我有2个XSL样式表,其中两个变换元件<Group id="all">包括XSL模板和合并输出

输出应该被合并,而是正在被main.xsltinclude.xslt覆盖。 (取决于订单)

我宁愿不修改include.xslt文件,因为它在其他样式表中共享,不应修改。

main.xslt

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:include href="include.xslt"/> 
    <xsl:template match="Group[@id='all']"> 
    <xsl:copy> 
     <xsl:copy-of select="@*|node()" /> 
     <xsl:apply-templates select="document('part1.xml')" /> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

include.xslt

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="Group[@id='all']"> 
    <xsl:copy> 
     <xsl:copy-of select="@*|node()" /> 
     <xsl:apply-templates select="document('part2.xml')" /> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

input.xml中

<?xml version="1.0"?> 

<Group id="all"> 
testdata below: 
</Group> 

part1.xml

<?xml version="1.0"?> 
<data id="test1"> 
Here is some test data. 
</data> 

part2.xml

<?xml version="1.0"?> 
<data id="test2"> 
Here is some more data. 
</data> 

实际输出:

<?xml version="1.0"?> 
<Group id="all"> 
testdata below: 

Here is some test data. 
</Group> 

预期输出:

<?xml version="1.0"?> 
<Group id="all"> 
testdata below: 

Here is some test data. 
Here is some more data. 
</Group> 

回答

0

正常的方式做,这是使用xsl:import代替xsl:include,然后添加一个xsl:apply-imports ...

main.xslt

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:import href="include.xslt"/> 
    <xsl:template match="Group[@id='all']"> 
    <xsl:copy> 
     <xsl:copy-of select="@*|node()" /> 
     <xsl:apply-templates select="document('part1.xml')" /> 
     <xsl:apply-imports/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

但是,你到底是什么了是二Group元素,一个嵌套在另一个里面(你可以在xsl:copy后移动xsl:apply-imports让他们一前一后).. 。

<Group id="all"> 
testdata below: 

Here is some test data. 
<Group id="all"> 
testdata below: 

Here is some more data. 
</Group></Group> 
从我所看到的你要么需要

所以(任选其一):

  • 过程的输出第二次做实际合并两个Group元素。
  • 使用类似node-set()(或使用XSLT 2.0)的扩展函数将Group结构保存在变量中,然后处理变量以合并Group
  • 修改include.xslt所以它不输出Group(或文本testdata below:)。
+0

感谢您的回答,不幸的是,使用导入不起作用,因为在包含文件中还有其他模板,这些模板都需要在'main.xslt'中指定。你有一个使用node-set()的例子吗? – 0x00

+0

我使用节点集解决了问题,稍后将发布解决方案 – 0x00