微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将多个xml文件转换并合并为一个文件

我希望你的帮助可以使用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.
>变换包括众所周知的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>

当应用于问题中提供的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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。