2017-04-12 49 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