2014-10-28 76 views
0

我有一個有兩個XML變量,下面是樣本數據是:合併兩個XML數據,並使用相同的XSLT輸出

$變量1:

<Group> 
    <A>Test</A> 
    <B>Test1</B> 
    ..... 
    ..... 
    ..... 
</Group> 

$變量2:

<Data> 
    <ABC>Test</ABC> 
    <XYZ>Test1</XYZ> 
    ..... 
    ..... 
    ..... 
</Data> 

現在我想在XSLT中合併這兩個變量,並在同一個XSLT中使用輸出,所以輸出將是s合併後如下所示:

<Group> 
    <A>Test</A> 
    <B>Test1</B> 
    ..... 
    ..... 
    ..... 

    <ABC>Test</ABC> 
    <XYZ>Test1</XYZ> 
    ..... 
    ..... 
    ..... 
</Group> 

上面的輸出將在相同的XSLT中傳遞以供進一步處理。

下面是XSLT樣品,我曾嘗試:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:param name="var1" select="document($Variable1)" /> 
    <xsl:param name="var2" select="document($Variable2)" /> 

    //Here I want to merge above to inputs and later will be used in XSLT below 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Group"> 
    ------- 
    ------- 
    ------- 
    ------- 
    </xsl:template> 
</xsl:stylesheet> 
+0

你的變量'Variable1'和'Variable2'從哪裏來?它們應該在樣式表中定義。 – 2014-10-28 17:46:02

+0

它來自C#代碼 – 2014-10-28 17:58:54

+0

「*它來自C#代碼*」這是不可能的。變量/參數必須在樣式表本身中定義。您只能將**值**傳遞給預定義的參數。 – 2014-10-29 05:36:14

回答

0

如果您採取以下樣式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="file1"/> 
<xsl:param name="file2"/> 

<xsl:template match="/"> 
    <xsl:apply-templates select="document($file1)/*"/> 
</xsl:template> 

<xsl:template match="/*"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <xsl:apply-templates select="document($file2)/*/*"/> 
    </xsl:copy> 
</xsl:template> 

<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

,並通過它的路徑到你的兩個文件(作爲字符串),將返回以下結果

<?xml version="1.0" encoding="UTF-8"?> 
<Group> 
    <A>Test</A> 
    <B>Test1</B> 
    <ABC>Test</ABC> 
    <XYZ>Test1</XYZ> 
</Group> 

當然,你還需要第三個(虛擬)XML在其上運行的轉換文件。更智能的實現將使用第一個輸入文件作爲源XML,並且只傳遞第二個文件的路徑作爲參數。

+0

抱歉,延遲響應.. ...在我的情況下,file1和file2是在運行時生成的XML字符串...他們不是我們可以使用的文件 ..任何建議 – 2014-11-20 07:38:16

+0

@ManojSingh沒有像「XML字符串」這樣的東西。一個字符串是一個字符串 - 並且使用XSLT處理字符串的功能不多。看到這個最近的問題:http://stackoverflow.com/questions/27018244/apply-transforms-to-xml-attribute-containing-escaped-html/27019850#27019850 – 2014-11-20 08:37:13

+0

是的,我知道沒有這樣的事情像XML字符串,我只是告訴file1和file2將有一個字符串值,它將在運行時生成。 – 2014-11-20 09:51:04

相關問題