2015-02-24 59 views
3

我無法找到一個合適的例子,以2個XML文件合併成1,如下面:結合2個XML文件轉換成1使用XSL

movies.xml

<movies> 
    <movie> 
     <name>ET</name> 
     <director>Steven Spielberg</director> 
     <date>15 January 2000</date> 
    </movie> 
    <movie> 
     <name>The Neverending Story</name> 
     <director>Steven Spielberg</director> 
     <date>4 November 2006</date> 
    </movie> 
</movies> 

directos.xml

<directors> 
    <director> 
     <name>Steven Spielberg</name> 
     <age>55</age> 
     <sex>male</sex> 
    </director> 
</directors> 

所需的輸出:

<directors> 
    <director> 
     <name>Steven Spielberg</name> 
     <age>55</age> 
     <sex>male</sex> 
     <movies> 
      <movie> 
       <name>ET</name> 
       <date>15 January 2000</date> 
      </movie> 
      <movie> 
       <name>The Neverending Story</name> 
       <date>4 November 2006</date> 
      <movie> 
     </movies> 
    </director> 
</directors> 

XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method = "xml" indent = "yes" /> 

    <xsl:variable name="movieFile" select="document('movies.xml')" /> 

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

    <xsl:template match="$movieFile/movies/movie/director"> 
     <xsl:copy> 
      <xsl:apply-templates /> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

目前,正在打印每部電影組內的整個導演名單。

有人請給我一些指導嗎?

+1

http://www.w3.org/TR/xslt#document – MIkCode 2015-02-24 10:31:01

+0

展我們到目前爲止已經嘗試過的XSLT代碼,我們很樂意幫助您進行調試,但是SO不會爲您寫出所有內容。 – 2015-02-24 10:31:19

+0

我已經添加了一些代碼@IanRoberts。我並不希望有人寫出所有的東西。希望在正確的方向推動:) – CocaCola 2015-02-24 11:08:22

回答

2

如果用這個代替你的第二個模板,喂「directors.xml」文件作爲輸入,它應該工作:

<xsl:template match="director"> 
    <xsl:variable name="director-name" select="name"/> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()" /> 
    <xsl:apply-templates select="document('movies.xml')/movies/movie[director=$director-name]" /> 
    </xsl:copy> 
</xsl:template> 

您複製使用身份模板的每個節點除了director節點之上,在其中添加movie文件的匹配內容。

+0

這很有效。真棒!謝謝@helderarocha – CocaCola 2015-02-24 12:40:25

1

如果你的確可以使用XSLT 2.0,那麼這可能是很簡單的:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> 

<xsl:variable name="movieFile" select="document('movies.xml')" /> 

<xsl:key name="movie-by-director" match="movie" use="director" /> 

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

<xsl:template match="director"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <movies> 
      <xsl:apply-templates select="key('movie-by-director', name, $movieFile)" /> 
     </movies> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

我添加了另一個導演和電影。它不編譯@ michael.hor257k – CocaCola 2015-02-24 12:39:30

+0

@CocaCola我不知道你的意思是「不編譯」。你爲什麼不提供你的實際(新)例子和你得到的確切結果? – 2015-02-24 13:06:29