如何使用匹配节点连接两个 XML 文件
我需要找到一种方法,当两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是对原来的改造稍加修改,以适应新的要求。合并是通过检查 file2.xml 元素来执行的。对于file1中的当前ITEM,仅如果file1中不存在子ITEM,则file2中的子ITEM将被合并。
[XSLT 1.0]
应用于此
input1.xml
:和
input2.xml
进行合并,产生:产生:
注意:
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]
Applied on this
input1.xml
:and
input2.xml
to merge, produces:produces:
Notice that:
input1.xml
is copied in output only if has a match ininput2.xml
此转换(
c:/temp/file1.xml
和c:/temp/file2.xml
如问题中提供):< strong>当应用于任何 XML 文档(未使用/忽略)时,会产生所需的正确结果:
说明:
首先我们处理(将模板应用于)第一个 XML 文档的节点。
身份规则/模板按原样复制每个节点。
有一个模板可以覆盖身份规则。此模板匹配任何名为
ITEM
的元素。它创建一个也名为ITEM
的元素,然后处理所有子节点,这会导致通过身份模板复制它们。最后,作为第二个 XML 文档中任何ITEM
元素子级的所有节点,其STYLE_COLOR
子级具有与STYLE_COLOR
STYLE_COLOR 的字符串值相同的字符串值当前(匹配)元素的 code> 子元素也会被复制(通过向其应用模板并作为选择和执行标识模板的结果),但STYLE_COLOR
子元素除外本身。请注意,两个 pat 的文件路径作为参数传递给转换,这使其更加灵活,并且能够处理任意两个 XML 文件 - 无需任何修改。我们使用 XSLT
document()
函数来加载和解析这两个文档,以便它们可以通过 xslt 转换进行处理。
请注意,此转换中未使用
xsl:for-each
或xsl:if
或任何其他 XSLT 条件指令。这使得代码更简单、更容易维护和理解。最后,请注意身份规则/模板的使用和覆盖。这是最基本、最强大的 XSLT 设计模式。
This transformation (
c:/temp/file1.xml
andc:/temp/file2.xml
are as provided in the question):when applied on any XML document (not used / ignored), produces the wanted, correct result:
Explanation:
First we process (apply templates to) the nodes of the first XML document.
The identity rule/template copies every node as is.
There is a single template that overrides the identity rule. This template matches any element named
ITEM
. It creates an element also namedITEM
, then processes all children nodes and this results in copying them by the identity template. Finally, all nodes that are children of anyITEM
element from the second XML document whoseSTYLE_COLOR
child has the same string value as the string value of theSTYLE_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 theSTYLE_COLOR
child itself.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.Do note that no
xsl:for-each
orxsl:if
or any other XSLT conditional instructions are used in this transformation. This results in simpler and easier to maintain and understand code.Finally, note the use and overriding of the identity rule/template. This is the most fundamental and powerful XSLT design pattern.
XSLT 非常强大,但是我必须承认我对它不太熟悉,所以会给您一个手动转换的建议:
您可以在进一步处理中轻松完成的任何其他事情,因为这是 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:
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.