如何将多个xml文件转换并合并为一个文件
发布时间:2020-12-16 22:59:39 所属栏目:百科 来源:网络整理
导读:我希望你的帮助可以使用XSLT将一些 XML转换为新的 XML.我有选择多个XML并在其上应用XSLT的代码. 我的问题是XSLT,我想将那些shop1.xml shop2xml ..转换为allshops.xml.对于知道如何使用XSLT的人来说,这是一项简单的任务,因为只有2-3个更改. 您可以在下面找到
我希望你的帮助可以使用XSLT将一些
XML转换为新的
XML.我有选择多个XML并在其上应用XSLT的代码.
我的问题是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 //这个产品直接以root身份生产,而且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> 解决方法
根据要求,这里是一个纯XSLT解决方案,它使用document()XSLT 1.0函数合并外部文件.
注意: >此转换的输入是paths.xml. [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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |