如何使用匹配节点连接两个 XML 文件

发布于 2024-11-15 18:30:07 字数 3721 浏览 0 评论 0原文

我需要找到一种方法,当两个 XML 文件具有匹配的节点时将它们连接起来。据我所知,这可以用许多不同的语言来完成......是否有 PHP 或 AJAX 方法来做到这一点?从 SO 上的其他帖子中我看到了 XSLT 解决方案..但我并没有真正理解。这是最好/首选的方法吗?如果是的话,知道任何有用的 XSLT 教程吗?

例如,XML-1 就像:

<FOO>
    </A>
    </B>
    </C>
    </D>
</FOO>

和 XML-2:

<FOO>    
    </B>
    </E>
</FOO>

检查 == 位置然后添加 的最佳方法是什么?代码>

更新

好吧,我无法让它与我的假设示例一起工作,并且我想我会更新我真正在做的事情,看看是否有人可以帮助我解决这个问题。我已经尝试过下面的方法以及我在 SO 上找到的其他方法,但没有运气。

真正的模式是这样的:

file1.xml

<?xml version="1.0"?>
<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
 ...
</DATA>

file2.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl" ?>
<DATA>
  <ITEM>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
  </ITEM>
  ....
</DATA>

我需要弄清楚的是生成一个新的 XML 文件,它将用相同的 SYTLE_COLOR 合并这些节点,看起来像:

<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
    <STATUS>Disabled</STATUS>
  </ITEM>

我尝试创建一个看起来像这样的 merge.xsl :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" />
  <xsl:output indent="yes"/>
  <xsl:variable name="with" select="'file-2.xml'" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="scene">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="info" select="document($with)/DATA/ITEM[STYLE_COLOR=current()/STYLE_COLOR]/." />
      <xsl:for-each select="$info/*">
        <xsl:if test="name()!='STYLE_COLOR'">
          <xsl:copy-of select="." />
        </xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

I还尝试了这样的合并:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:variable name="input2" select="document('file-2.xml')/DATA/ITEM"/>
    <xsl:template match="STYLE_COLOR">
        <xsl:copy>
            <xsl:apply-templates select="*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:choose>
            <xsl:when test="$input2/*[name()=name(current())]">
                <xsl:copy-of select="$input2/*"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet> 

这些方法都不起作用..抱歉,XSLT 对我来说很新,所以我不确定我在做什么,并且非常感谢有人帮助解决这个问题。

I need to find a way to join two XML files when they have a matching node. From what I gather this could be accomplished with many different languages... is there a PHP or an AJAX way to do this? From other posts on SO I see XSLT solutions.. that I dont really get. Is this the best/preferred method? If so, know of any helpful XSLT tutorials?

For example XML-1 is like :

<FOO>
    </A>
    </B>
    </C>
    </D>
</FOO>

and XML-2 :

<FOO>    
    </B>
    </E>
</FOO>

What would be the best approach for checking where <B>==<B> then add <E>

Update

Well I cant get this to work with my hypothetical example and thought I would update with what I am really doing to see if anyone can help me figure this. I have tried the methods from below and others I have found on SO with no luck.

The real schema is like :

file1.xml

<?xml version="1.0"?>
<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
 ...
</DATA>

file2.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="merge.xsl" ?>
<DATA>
  <ITEM>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
  </ITEM>
  ....
</DATA>

What I need to figure out is to have a new XML file generated that would merge these nodes with identical SYTLE_COLOR and look like:

<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
    <STATUS>Disabled</STATUS>
  </ITEM>

I tried creating a merge.xsl that looks like :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" />
  <xsl:output indent="yes"/>
  <xsl:variable name="with" select="'file-2.xml'" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="scene">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
      <xsl:variable name="info" select="document($with)/DATA/ITEM[STYLE_COLOR=current()/STYLE_COLOR]/." />
      <xsl:for-each select="$info/*">
        <xsl:if test="name()!='STYLE_COLOR'">
          <xsl:copy-of select="." />
        </xsl:if>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>
</xsl:transform>

I also tried a merge like this:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:variable name="input2" select="document('file-2.xml')/DATA/ITEM"/>
    <xsl:template match="STYLE_COLOR">
        <xsl:copy>
            <xsl:apply-templates select="*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:choose>
            <xsl:when test="$input2/*[name()=name(current())]">
                <xsl:copy-of select="$input2/*"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet> 

Neither of these methods are working.. sorry XSLT is very new to me so I am not sure what I am doing and would really appreciate some hand holding on this one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心病无药医 2024-11-22 18:30:07

这是对原来的改造稍加修改,以适应新的要求。合并是通过检查 file2.xml 元素来执行的。对于file1中的当前ITEM,如果file1中不存在子ITEM,则file2中的子ITEM将被合并。


[XSLT 1.0]

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="input2" select="document('test_input2.xml')/DATA"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ITEM">
        <xsl:variable name="item" select="
            $input2/ITEM[STYLE_COLOR=current()/STYLE_COLOR]"/>
        <xsl:variable name="ITEM" select="."/>

        <xsl:if test="$item">
            <xsl:copy>

                <xsl:for-each select="$item/*">
                    <xsl:if test="count($ITEM/*[name()=name(current())])=0">
                        <xsl:copy-of select="." />
                    </xsl:if>
                </xsl:for-each>

                <xsl:apply-templates select="*"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet> 

应用于此 input1.xml:

<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740008</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>777</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
</DATA>

input2.xml 进行合并,产生:

<DATA>
  <ITEM>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
    <CLASS_NAME>XXX</CLASS_NAME>
    <OTHER>YYY</OTHER>
  </ITEM>
  <ITEM>
    <STYLE_COLOR>1524740008</STYLE_COLOR>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
  </ITEM>
</DATA>

产生:

<DATA>
   <ITEM>
      <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
      <OTHER>YYY</OTHER>
      <PRODUCT_TYPE>simple</PRODUCT_TYPE>
      <STYLE_COLOR>1524740007</STYLE_COLOR>
      <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
      <CLASS_NAME>FOOTWEAR</CLASS_NAME>
      <STATUS>Disabled</STATUS>
   </ITEM>
   <ITEM>
      <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
      <PRODUCT_TYPE>simple</PRODUCT_TYPE>
      <STYLE_COLOR>1524740008</STYLE_COLOR>
      <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
      <CLASS_NAME>FOOTWEAR</CLASS_NAME>
      <STATUS>Disabled</STATUS>
   </ITEM>
</DATA>

注意:

  • 转换覆盖给定 ITEM 的现有元素,只需复制
  • input1.xml 中缺少的 ITEM 仅当在 input2.xml 中匹配时才会复制到输出中

This is the original transform slightly modified to adapt the new requirements. The merge is performed by checking against file2.xml elements. For the current ITEM in file1, a children ITEM in file2 will be merged only if not present in the file1.


[XSLT 1.0]

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="input2" select="document('test_input2.xml')/DATA"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ITEM">
        <xsl:variable name="item" select="
            $input2/ITEM[STYLE_COLOR=current()/STYLE_COLOR]"/>
        <xsl:variable name="ITEM" select="."/>

        <xsl:if test="$item">
            <xsl:copy>

                <xsl:for-each select="$item/*">
                    <xsl:if test="count($ITEM/*[name()=name(current())])=0">
                        <xsl:copy-of select="." />
                    </xsl:if>
                </xsl:for-each>

                <xsl:apply-templates select="*"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet> 

Applied on this input1.xml:

<DATA>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>1524740008</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
  <ITEM>
    <PRODUCT_TYPE>simple</PRODUCT_TYPE>
    <STYLE_COLOR>777</STYLE_COLOR>
    <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
    <CLASS_NAME>FOOTWEAR</CLASS_NAME>
    <STATUS>Disabled</STATUS>
  </ITEM>
</DATA>

and input2.xml to merge, produces:

<DATA>
  <ITEM>
    <STYLE_COLOR>1524740007</STYLE_COLOR>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
    <CLASS_NAME>XXX</CLASS_NAME>
    <OTHER>YYY</OTHER>
  </ITEM>
  <ITEM>
    <STYLE_COLOR>1524740008</STYLE_COLOR>
    <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
  </ITEM>
</DATA>

produces:

<DATA>
   <ITEM>
      <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
      <OTHER>YYY</OTHER>
      <PRODUCT_TYPE>simple</PRODUCT_TYPE>
      <STYLE_COLOR>1524740007</STYLE_COLOR>
      <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
      <CLASS_NAME>FOOTWEAR</CLASS_NAME>
      <STATUS>Disabled</STATUS>
   </ITEM>
   <ITEM>
      <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
      <PRODUCT_TYPE>simple</PRODUCT_TYPE>
      <STYLE_COLOR>1524740008</STYLE_COLOR>
      <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
      <CLASS_NAME>FOOTWEAR</CLASS_NAME>
      <STATUS>Disabled</STATUS>
   </ITEM>
</DATA>

Notice that:

  • the transform does not override existing elements for a given ITEM, just copy the missing ones
  • ITEM in input1.xml is copied in output only if has a match in input2.xml
我的鱼塘能养鲲 2024-11-22 18:30:07

此转换c:/temp/file1.xmlc:/temp/file2.xml 如问题中提供):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pDoc1" select="'file:///c:/temp/file1.xml'"/>
 <xsl:variable name="vDoc1" select="document($pDoc1)"/>

 <xsl:param name="pDoc2" select="'file:///c:/temp/file2.xml'"/>
 <xsl:variable name="vDoc2" select="document($pDoc2)"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:apply-templates select="$vDoc1/node()"/>
 </xsl:template>

 <xsl:template match="ITEM">
  <ITEM>
   <xsl:apply-templates/>

   <xsl:apply-templates select=
     "$vDoc2/*/ITEM
               [STYLE_COLOR = current()/STYLE_COLOR]
                /node()[not(self::STYLE_COLOR)]
     "/>
  </ITEM>
 </xsl:template>
</xsl:stylesheet>

< strong>当应用于任何 XML 文档(未使用/忽略)时,会产生所需的正确结果:

<DATA>
   <ITEM xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <PRODUCT_TYPE>simple</PRODUCT_TYPE>
      <STYLE_COLOR>1524740007</STYLE_COLOR>
      <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
      <CLASS_NAME>FOOTWEAR</CLASS_NAME>
      <STATUS>Disabled</STATUS>
      <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
   </ITEM> ...
</DATA>

说明

  1. 首先我们处理(将模板应用于)第一个 XML 文档的节点

  2. 身份规则/模板按原样复制每个节点

  3. 有一个模板可以覆盖身份规则。此模板匹配任何名为 ITEM 的元素。它创建一个也名为 ITEM 的元素,然后处理所有子节点,这会导致通过身份模板复制它们。最后,作为第二个 XML 文档中任何 ITEM 元素子级的所有节点,其 STYLE_COLOR 子级具有与 STYLE_COLORSTYLE_COLOR 的字符串值相同的字符串值当前(匹配)元素的 code> 子元素也会被复制(通过向其应用模板并作为选择和执行标识模板的结果),但 STYLE_COLOR 子元素除外本身。

  4. 请注意,两个 pat 的文件路径作为参数传递给转换,这使其更加灵活,并且能够处理任意两个 XML 文件 - 无需任何修改。我们使用 XSLT document()函数来加载和解析这两个文档,以便它们可以通过 xslt 转换进行处理。

  5. 请注意,此转换中未使用 xsl:for-eachxsl:if 或任何其他 XSLT 条件指令。这使得代码更简单、更容易维护和理解。

  6. 最后,请注意身份规则/模板的使用和覆盖。这是最基本、最强大的 XSLT 设计模式

This transformation (c:/temp/file1.xml and c:/temp/file2.xml are as provided in the question):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pDoc1" select="'file:///c:/temp/file1.xml'"/>
 <xsl:variable name="vDoc1" select="document($pDoc1)"/>

 <xsl:param name="pDoc2" select="'file:///c:/temp/file2.xml'"/>
 <xsl:variable name="vDoc2" select="document($pDoc2)"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:apply-templates select="$vDoc1/node()"/>
 </xsl:template>

 <xsl:template match="ITEM">
  <ITEM>
   <xsl:apply-templates/>

   <xsl:apply-templates select=
     "$vDoc2/*/ITEM
               [STYLE_COLOR = current()/STYLE_COLOR]
                /node()[not(self::STYLE_COLOR)]
     "/>
  </ITEM>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document (not used / ignored), produces the wanted, correct result:

<DATA>
   <ITEM xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <PRODUCT_TYPE>simple</PRODUCT_TYPE>
      <STYLE_COLOR>1524740007</STYLE_COLOR>
      <SHORT_DESCRIPTION>Black Shoe</SHORT_DESCRIPTION>
      <CLASS_NAME>FOOTWEAR</CLASS_NAME>
      <STATUS>Disabled</STATUS>
      <NEXT_ARRIVAL>2011-08-05</NEXT_ARRIVAL>
   </ITEM> ...
</DATA>

Explanation:

  1. First we process (apply templates to) the nodes of the first XML document.

  2. The identity rule/template copies every node as is.

  3. There is a single template that overrides the identity rule. This template matches any element named ITEM. It creates an element also named ITEM, then processes all children nodes and this results in copying them by the identity template. Finally, all nodes that are children of any ITEM element from the second XML document whose STYLE_COLOR child has the same string value as the string value of the STYLE_COLOR child of the current (matched) element are also copied (by applying templates to them and as result of the selection and execution of the identity template), with the exception of the STYLE_COLOR child itself.

  4. Do note that the filepaths of the two pats are passed as parameters to the transformation, which makes it more flexible and able to work with any two XML files -- without any modification. We use the XSLT document() function to load and parse these two documents so that they can be processed by the xslt transformation.

  5. Do note that no xsl:for-each or xsl:if or any other XSLT conditional instructions are used in this transformation. This results in simpler and easier to maintain and understand code.

  6. Finally, note the use and overriding of the identity rule/template. This is the most fundamental and powerful XSLT design pattern.

花想c 2024-11-22 18:30:07

XSLT 非常强大,但是我必须承认我对它不太熟悉,所以会给您一个手动转换的建议:

print "<FOOCONTAINER>\n";
readfile($xml1file);
readfile($xml2file);
print "</FOOCONTAINER>\n";

您可以在进一步处理中轻松完成的任何其他事情,因为这是 XML。

编辑:这仅适用于 OP 提供的第一个 XML。

XSLT is quite powerful, however I must admit that I'm not that fluently with it, so will give you a sugestion for a manual transformation:

print "<FOOCONTAINER>\n";
readfile($xml1file);
readfile($xml2file);
print "</FOOCONTAINER>\n";

Anything else you can easily accomplish within the further processing as this is XML.

Edit: This works only for the first XML offered by the OP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文