私はマージする必要がある2つのXMLドキュメントを持っています。すべての要素には定義済みのIDがあります。要素が両方の文書で一意である場合 - >結果に追加されますが、そうでない場合は - >属性がマージされます。 ELでXSLTは2つのドキュメントの要素の属性を結合します
main.xml
<main>
<el id="1" attr1="value1" />
<el id="2" attr2="value2" default-attr="def" />
</main>
snippet.xml
<main>
<el id="2" attr2="new value2" new-attr="some value" />
<el id="3" attr3="value3" />
</main>
result.xml
<main>
<el id="1" attr1="value1" />
<el id="2" attr2="new value2" default-attr="def" new-attr="some value" />
<el id="3" attr3="value3" />
</main>
属性[@ id = 2]はマージされ、値はsnippet.xmlから上書きされます。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="snippetDoc" select="document(snippet.xml)" />
<xsl:template match="@* | node() | comment()">
<xsl:copy>
<xsl:apply-templates select="@* | node() | comment()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="el">
<xsl:copy>
<!-- how to distinguish between @ids of two documents? -->
<xsl:copy-of select="$snippetDoc/main/el/[@id = @id]/@*" />
<xsl:apply-templates select="@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
は、しかし、それは2つの文書に同じ属性を区別できるようにする必要があり
merge.xlst:
私はこれを試してみました。さらに、これはsnippet.xmlから一意の要素をコピーしません。
ありがとうございました!