2つのxmlファイルを連結したい。 input1.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からノードが存在する場合はそれを結合し、存在しない場合は表示される順序に従って下部に配置します。
どうもありがとう。
ジョン