我有两个输入文件:file1.xml 和 file2.xml,结构相同但内容不同(节点source
和target
节点)。
file1.xml(简化版)
<?xml version="1.0" encoding="UTF-8"?>
<xliff>
<file>
<body>
<trans-unit id="MDSD_0">
<source>Gestioni els seus favorits</source>
<target>Gestioni els seus favorits</target>
</trans-unit>
<trans-unit id="MDSD_1">
<source>Favorits</source>
<target>Favorits</target>
</trans-unit>
</body>
</file>
</xliff>
file2.xml(简化版)
<?xml version="1.0" encoding="UTF-8"?>
<xliff>
<file>
<body>
<trans-unit id="MDSD_0">
<source>Manage your bookmarks</source>
<target>Manage your bookmarks</target>
</trans-unit>
<trans-unit id="MDSD_1">
<source>Bookmarks</source>
<target>Bookmarks</target>
</trans-unit>
</body>
</file>
</xliff>
我想从 file1.xml 中获取除源节点之外的所有内容,我想从 file2.xml 中获取这些内容。换句话说,我想用source
file2.xml 替换file1.xmlsource
中的内容。
我很想在 Perl 或 PHP 中这样做,但我认为在 XSLT 中它会更有效。但是,我有点卡住了。
我的样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="source">
<source>
<xsl:value-of select="document('file2.xlf')//source" />
</source>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这会产生以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<xliff>
<file>
<body>
<trans-unit id="MDSD_0">
<source>Manage your bookmarks</source>
<target>Gestioni els seus favorits</target>
</trans-unit>
<trans-unit id="MDSD_1">
<source>Manage your bookmarks</source> <!-- this one is wrong -->
<target>Favorits</target>
</trans-unit>
</body>
</file>
</xliff>
如您所见,它仅使用 file2.xml 中第一个源节点的内容来替换 file1.xml 中的所有源节点。
我想我需要根据位置或id
父级trans-unit
相同的位置以某种方式进行选择。我试过了
<xsl:value-of select="document('file2.xlf')//source/parent::trans-unit[@id= current()]" />
但这给了我<source/>
。
我会很感激任何提示。
我的样式表是 XSLT 版本 1,但我想我可以在必要时使用 XLST 2.0(我使用的是 Oxygen 和 Saxon 的免费版本)。