我想連接兩個xml文件。 這裏是input1.xml:如何解決這個兩個XML文件的串聯?
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
</nodeB>
</section>
</sequence>
</schema>
這裏input2.xml
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete" />
</nodeA>
<nodeA id="b">
<fruit id="small">
<lime id="x" method="create">
<attributes>
<color>Yellow</color>
<year>2001</year>
</attributes>
</lime>
</fruit>
</nodeA>
<nodeB id="b">
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
<nodeB id="c">
<dog id="small">
<terrier id="x" method="delete" />
</dog>
</nodeB>
</section>
</sequence>
</schema>
我的輸出:
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete"/>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
</section>
</sequence>
</schema>
雖然預期輸出是:
<schema>
<sequence>
<section id="xxx">
<nodeA id="a">
<fruit id="small">
<orange id="x" method="create">
<attributes>
<color>Orange</color>
<year>2000</year>
</attributes>
</orange>
</fruit>
<fruit id="small">
<melon id="x" method="create">
<attributes>
<color>Green</color>
</attributes>
</melon>
</fruit>
<lemon id="z" method="delete"/>
</nodeA>
<nodeB id="b">
<dog id="large">
<doberman id="x" method="create">
<condition>
<color>Black</color>
</condition>
</doberman>
</dog>
<dog id="small">
<poodle id="x" method="create">
<condition>
<color>White</color>
</condition>
</poodle>
</dog>
</nodeB>
<nodeA id="b"> <!-- I'm missing this node -->
<fruit id="small">
<lime id="x" method="create">
<attributes>
<color>Yellow</color>
<year>2001</year>
</attributes>
</lime>
</fruit>
</nodeA>
<nodeB id="c"> <!-- I'm missing this node -->
<dog id="small">
<terrier id="x" method="delete" />
</dog>
</nodeB>
</section>
</sequence>
</schema>
的XSLT文件是這樣的:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://a.com">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" method="xml" />
<xsl:param name="input2"/>
<xsl:variable name="to-merge" select="document($input2)" />
<xsl:function name="a:id">
<xsl:param name="ctx"/>
<xsl:value-of select="concat($ctx/local-name(), $ctx/@id)"/>
</xsl:function>
<xsl:key name="match" match="/schema/sequence/section/*" use="a:id(.)"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[count(. | key('match', a:id(.))) = count(key('match', a:id(.)))]">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:variable name="id" select="a:id(.)"/>
<xsl:for-each select="$to-merge">
<xsl:apply-templates select="key('match', $id)/*"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如何修改XSLT文件來生成所需的輸出?這裏的關鍵是保持節點的順序。如果節點存在於file1中,我們將它合併,如果不是,我們會根據它們的顯示順序將它放在最下面。
非常感謝。
約翰
您在您的問題中使用的XML既沒有意義也沒有基於錯誤的設計。我會一直用這樣的XML來忽略問題。特別是,它太長了,屬性沒有意義,特定元素的名稱是任意的,不重複,有多個元素具有相同的'id'。這使得很難推斷出共同的結構和/或關係。我希望你們能夠改進未來問題的例子,使它們不再是無意義的,非結構化的,違反基本原則和混亂的。 –
@DimitreNovatchev我爲此道歉。這不是我打算給出令人困惑的例子,它只是它正在與我一起工作的真正的XML結構(與重複的ID和東西)。但我會在未來嘗試提供更有意義的例子。謝謝。 – John