繁体   English   中英

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

[英]How to transform and merge multiple xml files into a single one

我希望你的帮助可以使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM