1
我有兩個需要合併的XML文檔。每個元素都有一個定義的ID。如果一個元素在兩個文檔中都是唯一的 - >將被添加到結果中,否則 - 屬性將被合併。XSLT合併兩個文檔中元素的屬性
main.xml中
<main>
<el id="1" attr1="value1" />
<el id="2" attr2="value2" default-attr="def" />
</main>
snippet.xml在EL
<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覆蓋值。
我已經試過這樣:
merge.xlst
<?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>
但它需要能夠在兩筆相同的屬性來區分。更重要的是,這不會複製snippet.xml中的獨特元素。
感謝您的幫助!