How to transform and merge multiple xml files into a single one
I want your help to transform some XML to a new one using XSLT. I have the code of selecting multiple XMLs and applying on them the XSLT.
My problem is with the XSLT where I want to convert those shop1.xml shop2xml .. to allshops.xml. It is an easy task for someone who knows how to work with XSLT because there only 2-3 changes.
Below you can find the structures for better understanding. Thank you very much.
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 //this one has directly products as root and id might be already present
<products>
<product>
<id>123</id>
<title></title>
<description></description>
<price></price>
<image></image>
<url></url>
<category id="62"></category>
</product>
</products>
paths.xml it is used from the php code to get multiple xml files
<?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 开发者_C百科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>
As requested, here a pure XSLT solution which merges the external files using the document()
XSLT 1.0 function.
Note:
- the input to this transform is
paths.xml
. - the transform include the well known Identity Transformation.
[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>
When applied on paths.xml
file provided in the question, produces:
<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>
精彩评论