我有两个不同的 xml(唯一的参考是 @id)。当@id 匹配时,需要将B.xml 的“transunit/target”的所有子节点复制到A.xml 的节点。请查看 A.xml 和 B.xml。
注意:B.xml中的所有目标内容都以“Translated”为后缀
XSLT 2.0 版和处理器 Saxon EE/HE 9.XXX
一个.xml
<concept id="001" xml:lang="en-us">
<title id="002">Notice</title>
<shortdesc id="003">This information U.S.A.</shortdesc>
<conbody id="004">
<p id="005">This product blah blah <companyname id="006">blah bla</companyname> No other warranty expressed or implied. </p>
<p id="007">This supersedes all previous notices.</p>
<section id="008">
<title id="009">COPYRIGHT LICENSE:</title>
<p id="010">This information contains <b id="011">in source language</b> , blah blah</p>
</section>
</conbody>
</concept>
B.xml
<root>
<transunit id="002">
<source>Notice</source>
<target>Translated:Notice</target>
</transunit>
<transunit id="003">
<source>This information U.S.A.</source>
<target>Translated:This information U.S.A.</target>
</transunit>
<transunit id="005">
<source>This product blah blah <companyname id="006" local-name="companyname">blah bla</companyname> No other warranty expressed or implied. </source>
<target>Translated:This product blah blah <companyname id="006" local-name="companyname">blah bla</companyname> No other warranty expressed or implied. </target>
</transunit>
<transunit id="007">
<source>This supersedes all previous notices.</source>
<target>Translated:This supersedes all previous notices.</target>
</transunit>
<transunit id="009">
<source>COPYRIGHT LICENSE:</source>
<target>Translated:COPYRIGHT LICENSE:</target>
</transunit>
<transunit id="010">
<source>This information contains <b id="011" local-name="b">in source language</b> , blah blah</source>
<target>Translated:This information contains <b id="011" local-name="b">in source language</b> , blah blah</target>
</transunit>
</root>
预期结果:
<concept id="001" xml:lang="en-us">
<title id="002">Translated:Notice</title>
<shortdesc id="003">Translated:This information U.S.A.</shortdesc>
<conbody id="004">
<p id="005">Translated:This product blah blah <companyname id="006">blah bla</companyname> No other warranty expressed or implied. </p>
<p id="007">Translated:This supersedes all previous notices.</p>
<section id="008">
<title id="009">Translated:COPYRIGHT LICENSE:</title>
<p id="010">Translated:This information contains <b id="011">in source language</b> , blah blah</p>
</section>
</conbody>
</concept>
我正在尝试类似的东西:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:template match="node()">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[count(ancestor::*) != 0]" priority="2">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:variable name="id">
<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="document('B.xml')//*[$id=@id]">
<xsl:for-each select="document('B.xml')//*[$id=@id]/target">
<xsl:message>i m hre</xsl:message>
<xsl:apply-templates/>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>