如何将多个xml文件转换并合并为一个 [英] How to transform and merge multiple xml files into a single one

查看:54
本文介绍了如何将多个xml文件转换并合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您的帮助,使用 XSLT 将一些 XML 转换为新的 XML.我有选择多个 XML 并应用 XSLT 的代码.

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.

我的问题是 XSLT,我想将那些 shop1.xml shop2xml .. 转换为 allshops.xml.对于知道如何使用 XSLT 的人来说,这是一项简单的任务,因为只有 2-3 处更改.

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//这个有root直接产品,id可能已经存在

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 用于从php代码中获取多个xml文件

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 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 函数合并外部文件.

As requested, here a pure XSLT solution which merges the external files using the document() XSLT 1.0 function.

注意:

  • 此转换的输入是 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 文件时,产生:

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>

这篇关于如何将多个xml文件转换并合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆