0

我有两个输入文件:file1.xml 和 file2.xml,结构相同但内容不同(节点sourcetarget节点)。

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 中获取这些内容。换句话说,我想用sourcefile2.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 的免费版本)。

4

2 回答 2

1

假设您想source通过匹配idparent 来查找值trans-unit,您可以执行以下操作:

<xsl:value-of select="document('file2.xml')/xliff/file/body/trans-unit[@id=current()/../@id]/source" />

在 XSLT 2.0 中,您可以通过将定义为:

<xsl:key name="src" match="source" use="../@id" />

然后将其用作:

<xsl:value-of select="key('src', ../@id, document('file2.xml'))" />
于 2017-06-18T11:07:15.667 回答
1

改变

<xsl:template match="source">
        <source>
            <xsl:value-of select="document('file2.xlf')//source" />
        </source>
</xsl:template>

<xsl:template match="source">
        <xsl:copy-of select="key('ref', ../@id, document('file2.xlf'))/source" />
</xsl:template>

添加<xsl:key name="ref" match="trans-unit" use="@id"/>到样式表中(并确保在 oXygen 中使用 Saxon 9 以获得 XSLT 2.0 支持)。

于 2017-06-18T11:07:22.817 回答