2011-07-26 160 views
4

我希望您的幫助可以使用XSLT將某些XML轉換爲新的XML文件。我有選擇多個XML並在其上應用XSLT的代碼。如何將多個xml文件轉換併合併爲一個文件

我的問題是在XSLT中,我想將shop1.xml shop2xml ..轉換爲allshops.xml。對於知道如何使用XSLT的人來說,這是一件容易的事,因爲只有2-3個變化。

下面您可以找到更好理解的結構。非常感謝你。

shop1.xml

<shop> 
    <products> 
     <product id="189"> 
      <title></title> 
      <description></description> 
      <price></price> 
      <image></image> 
      <url></url> 
      <category id="61"></category> 
     </product> 
    </products> 
</shop> 

shop2.xml

<shop> 
    <products>  
     <product id="182"> 
      <title></title> 
      <description></description> 
      <price></price> 
      <image></image> 
      <url></url> 
      <category id="62"></category> 
     </product> 
    </products> 
</shop> 

shop3.xml //這其中有直接的產品爲根,ID可能是已經存在

<products> 
    <product> 
     <id>123</id> 
     <title></title> 
     <description></description> 
     <price></price> 
     <image></image> 
     <url></url> 
     <category id="62"></category> 
    </product>  
</products> 

paths.xml它從PHP代碼用於獲取多個XML文件

<?xml version="1.0" encoding="utf-8"?> 
<files> 
    <file>shop1.xml</file> 
    <file>shop2.xml</file> 
    <file>shop3.xml</file> 
</files> 

allshops.xml

<products> //removed the store,shop and products stays as root 
    <product> 
     <id>the product's attribute id</id> //new element, with value the product id="" 
     <title></title> 
     <description></description> 
     <price></price> 
     <image></image> 
     <url></url> 
     <category></category> //removed the attribute id 
     <shopid></shopid> //new element, will be blank for now 
    </product> 
    <product> 
    </product> 
    . 
    . 
    . 
</products> 
+0

這將是更好使用'文檔加載的所有文件( )'XSLT 1.0函數。通過這種方式,您將能夠正確地將內容包含在一個'products'root中,並且可以用當前文件名填充'shopid'。 –

+0

你好。如果您需要回復,我對此解決方案非常感興趣。 – EnexoOnoma

+0

看到我的答案。我保留了一般變換,以便包含最終不包含在輸入樣本中的其他節點。希望能幫助到你。 –

回答

6

按照要求,在這裏它融合了外部純XSLT解決方案使用document() XSLT 1.0函數的文件。

注:


[XSLT 1.0]

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

    <xsl:template match="/*"> 
     <products> 
      <xsl:for-each select="file"> 
       <xsl:apply-templates 
        select="document(.)/*//product"> 
        <xsl:with-param name="file" select="."/> 
       </xsl:apply-templates> 
      </xsl:for-each> 
     </products> 
    </xsl:template> 

    <xsl:template match="product"> 
     <xsl:param name="file"/> 
     <xsl:copy> 

      <xsl:apply-templates select="@*"/> 

      <xsl:if test="not(id)"> 
       <id><xsl:value-of select="@id"/></id> 
      </xsl:if> 

      <xsl:apply-templates select="node()"/> 

      <shopid><xsl:value-of select="$file"/></shopid> 

     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="category/@id 
     | product/@id"/> 

</xsl:stylesheet> 

當在問題提供paths.xml文件被應用時產生:

<products> 
    <product> 
     <id>189</id> 
     <title/> 
     <description/> 
     <price/> 
     <image/> 
     <url/> 
     <category/> 
     <shopid>shop1.xml</shopid> 
    </product> 
    <product> 
     <id>1418</id> 
     <title/> 
     <description/> 
     <price/> 
     <image/> 
     <url/> 
     <category/> 
     <shopid>shop1.xml</shopid> 
    </product> 
    <product> 
     <id>182</id> 
     <title/> 
     <description/> 
     <price/> 
     <image/> 
     <url/> 
     <category/> 
     <shopid>shop2.xml</shopid> 
    </product> 
    <product> 
     <id>118</id> 
     <title/> 
     <description/> 
     <price/> 
     <image/> 
     <url/> 
     <category/> 
     <shopid>shop2.xml</shopid> 
    </product> 
</products> 
+0

@尼古拉,我很抱歉'類別',我誤解了你的問題。編輯的答案現在修復它。對於第二個問題,您的意思是說,您的某些Feed中有直接「產品」作爲根元素?如果你擴大你的原始問題並添加這些新信息並儘可能清楚,那將會更好。我會盡快回復。 –

+0

@Nikolai,無論如何,我已經概括了用不同根元素管理其他提要的答案。 –

+0

對不起,我再次檢查了我的一個Feed。我看到一些飼料不像'<產品ID = 「123」>''但 123> ...'這就是爲什麼有一個額外的''下面的''對轉換後的XML。所以如果我們有一個if子句,它檢查是否有一個''然後創建''否則如果是''不創建它,但繼續下一個元素。 – EnexoOnoma

相關問題