首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT:将同级文本节点移动到选定节点以进行XLIFF修复

XSLT:将同级文本节点移动到选定节点以进行XLIFF修复
EN

Stack Overflow用户
提问于 2013-01-09 08:11:22
回答 1查看 331关注 0票数 2

经过几个小时对XSLT的研究,我承认失败了!我需要修复从一个未命名的翻译工具返回给我们的大量.xlf XLIFF翻译文件。理想情况下,我将使用批处理工具对它们应用XSL转换。

下面是其中一个XLIFF文件的片段:

代码语言:javascript
复制
<body>
    <trans-unit id="1" phase-name="pretrans" restype="x-h3">
        <source>Adding, Deleting or Modifying Notes in the Call Description</source>
        <seg-source>Adding, Deleting or Modifying Notes in the Call Description</seg-source>
        <target state="final">Добавление, удаление и изменение примечаний в описании звонка</target>
    </trans-unit>
    <trans-unit id="2" phase-name="pretrans" restype="x-p">
        <source>Description of Fields on RHS</source>
        <seg-source>Description of Fields on RHS</seg-source>
        <target state="final">Поле описания в правой части</target>
    </trans-unit>
    <trans-unit id="3" phase-name="pretrans" restype="x-p">
        <source>You can add descriptive text notes to a call recording, if you have the appropriate privileges to do so. These notes are visible to all users who have access to the call recording. It is recommended that each user add their initials to the notes to avoid potential confusion.</source>
        <seg-source>
            <mrk mtype="seg" mid="1">You can add descriptive text notes to a call recording, if you have the appropriate privileges to do so.</mrk>
            <mrk mtype="seg" mid="2">These notes are visible to all users who have access to the call recording.</mrk>
            <mrk mtype="seg" mid="3">It is recommended that each user add their initials to the notes to avoid potential confusion.</mrk>
        </seg-source>
        <target state="final">
          <mrk mtype="seg" mid="1" /><ph ctype="" id="1">&lt;MadCap:variable name="zoom_userdocs_variables.var_product_name" xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" /&gt;</ph> позволяет находить телефонные взаимодействия, содержащие или не содержащие определенные фразы.
          <mrk mtype="seg" mid="2" />Каждая речевая метка содержит одну или несколько таких фраз.
          <mrk mtype="seg" mid="3" />Ядро <ph ctype="" id="3">&lt;MadCap:variable name="zoom_userdocs_variables.var_product_name" xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" /&gt;</ph> индексирует медиафайлы и помечает места вхождения фразы (добавляет к ним метки).
          <mrk mtype="seg" mid="4" />Затем нужные медиафайлы можно искать по связанным с ними меткам.
        </target>
    </trans-unit>
    <trans-unit id="4" phase-name="pretrans" restype="x-p">
        <source>To add, delete, or modify text in the description field, click inside the description field.</source>
        <seg-source>To add, delete, or modify text in the description field, click inside the description field.</seg-source>
        <target state="final">Чтобы добавить, удалить или изменить текст в поле описания, щелкните это поле.</target>
    </trans-unit>
</body>

请注意第三个trans-unit节点中的target标记。mrk标记应该包含现在已经成为兄弟节点的文本节点(与早期的seg-source标记相比,这仍然是正确的),这会扰乱结构。

因此,我尝试识别任何不包含文本节点的mrk标记,并将下面的文本节点移回其中。

以下是所需的结果:

代码语言:javascript
复制
<body>
    <trans-unit id="1" phase-name="pretrans" restype="x-h3">
        <source>Adding, Deleting or Modifying Notes in the Call Description</source>
        <seg-source>Adding, Deleting or Modifying Notes in the Call Description</seg-source>
        <target state="final">Добавление, удаление и изменение примечаний в описании звонка</target>
    </trans-unit>
    <trans-unit id="2" phase-name="pretrans" restype="x-p">
        <source>Description of Fields on RHS</source>
        <seg-source>Description of Fields on RHS</seg-source>
        <target state="final">Поле описания в правой части</target>
    </trans-unit>
    <trans-unit id="3" phase-name="pretrans" restype="x-p">
        <source>You can add descriptive text notes to a call recording, if you have the appropriate privileges to do so. These notes are visible to all users who have access to the call recording. It is recommended that each user add their initials to the notes to avoid potential confusion.</source>
        <seg-source>
            <mrk mtype="seg" mid="1">You can add descriptive text notes to a call recording, if you have the appropriate privileges to do so.</mrk>
            <mrk mtype="seg" mid="2">These notes are visible to all users who have access to the call recording.</mrk>
            <mrk mtype="seg" mid="3">It is recommended that each user add their initials to the notes to avoid potential confusion.</mrk>
        </seg-source>
        <target state="final">
            <mrk mtype="seg" mid="1"><ph ctype="" id="1">&lt;MadCap:variable name="zoom_userdocs_variables.var_product_name" xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" /&gt;</ph> позволяет находить телефонные взаимодействия, содержащие или не содержащие определенные фразы.</mrk>
            <mrk mtype="seg" mid="2">Каждая речевая метка содержит одну или несколько таких фраз.</mrk>
            <mrk mtype="seg" mid="3">Ядро <ph ctype="" id="3">&lt;MadCap:variable name="zoom_userdocs_variables.var_product_name" xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" /&gt;</ph> индексирует медиафайлы и помечает места вхождения фразы (добавляет к ним метки).</mrk>
            <mrk mtype="seg" mid="4">Затем нужные медиафайлы можно искать по связанным с ними меткам.</mrk>
        </target>
    </trans-unit>
    <trans-unit id="4" phase-name="pretrans" restype="x-p">
        <source>To add, delete, or modify text in the description field, click inside the description field.</source>
        <seg-source>To add, delete, or modify text in the description field, click inside the description field.</seg-source>
        <target state="final">Чтобы добавить, удалить или изменить текст в поле описания, щелкните это поле.</target>
    </trans-unit>
</body>

我通常会用LibXML或类似的语言在Perl中完成这项工作,但我确信这对于XSLT来说是一个简单的任务。我一直在寻找类似的解决方案,但找不到任何我可以工作的东西。

另一点需要注意的是,尽管这里“打印得很好”,但最终的body节点定义只占一行。

谢谢!我期待着学习新的东西!

编辑:更新了上面的源代码,以显示<target>元素中的更多子标记,这些标记必须保留。编辑2:添加所需的结果。

EN

回答 1

Stack Overflow用户

发布于 2013-01-09 08:39:34

试试这个XSLT:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="trans-unit/target/mrk[following-sibling::text()]">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
      <xsl:value-of select="following-sibling::text()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="trans-unit/target/text()"/>

</xsl:stylesheet>

它可能会产生预期的结果:

代码语言:javascript
复制
<body>
    <trans-unit id="1" phase-name="pretrans" restype="x-h3">
        <source>Adding, Deleting or Modifying Notes in the Call Description</source>
        <seg-source>Adding, Deleting or Modifying Notes in the Call Description</seg-source>
        <target state="final" />
    </trans-unit>
    <trans-unit id="2" phase-name="pretrans" restype="x-p">
        <source>Description of Fields on RHS</source>
        <seg-source>Description of Fields on RHS</seg-source>
        <target state="final" />
    </trans-unit>
    <trans-unit id="3" phase-name="pretrans" restype="x-p">
        <source>You can add descriptive text notes to a call recording, if you have the appropriate privileges to do so. These notes are visible to all users who have access to the call recording. It is recommended that each user add their initials to the notes to avoid potential confusion.</source>
        <seg-source>
            <mrk mtype="seg" mid="1">You can add descriptive text notes to a call recording, if you have the appropriate privileges to do so.</mrk>
            <mrk mtype="seg" mid="2">These notes are visible to all users who have access to the call recording.</mrk>
            <mrk mtype="seg" mid="3">It is recommended that each user add their initials to the notes to avoid potential confusion.</mrk>
        </seg-source>
        <target state="final"><mrk mtype="seg" mid="1">При наличии соответствующих прав можно добавить описательные текстовые примечания к записи звонка.
            </mrk><mrk mtype="seg" mid="2">Эти примечания видны для всех пользователей, которые имеют доступ к записи звонка.
            </mrk><mrk mtype="seg" mid="3">Во избежание возможной путаницы каждому пользователю рекомендуется к примечаниям добавлять свои инициалы.
        </mrk></target>
    </trans-unit>
    <trans-unit id="4" phase-name="pretrans" restype="x-p">
        <source>To add, delete, or modify text in the description field, click inside the description field.</source>
        <seg-source>To add, delete, or modify text in the description field, click inside the description field.</seg-source>
        <target state="final" />
    </trans-unit>
</body>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14226315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档