导入工具

XPath (TMS)

文本由 Phrase Language AI 从英语机器翻译而得。

XPath 代表 XML 路径语言。它可用于在 XML 文档中导航元素和属性。如果对 XPATH 不熟悉,请先查看 XPath 教程 并学习官方 XPATH 文档

AI 聊天机器人可以非常有效地生成和验证 Xpath。

支持 XPath 1.0 的一个子集,但有以下限制:

  • 步骤中的轴

    • 支持:

      ancestor, ancestor-or-self, attribute, 子, descendant, descendant-or-self

    • 不支持:

      following, preceding, following-sibling, preceding-sibling, namespace

  • 谓词

    • 支持:

      当前节点或祖先节点及其属性(属性、命名空间)上的条件

    • 不支持(例如)

      位置编号,轴 child::,descendant,descendant-or-self,following::,preceding::,following-sibling::,preceding-sibling::,函数 last()

基本规则

  • 使用 /// 在路径中

  • 使用单引号 ' ' 在名称中

  • 使用管道符 | 来连接请求

  • 名称区分大小写:<Body><body> 不同

示例

XPath 示例 1XPath 示例 2(带命名空间)是以下内容的示例文件:

  1. 导入所有元素和所有属性

    //* | //@*

  2. 导入所有元素和 attribute1 的值

    (<elem1 attribute1="translate" attribute2="Do not translate"/>)

    //* | //@attribute1

  3. 导入子元素的所有后代

    //Child//*

  4. 仅在属性 translate='true' 时导入元素 lis 及其后代 

    (<lis translate="true">translate this</lis><lis translate="false">do not translate this</lis>)

    //lis[@translate='true']/descendant-or-self::*

  5. 如果元素的属性为 translate='true',则导入所有元素和后代

    //*[@translate='true']/descendant-or-self::*

  6. 导入元素 Data 中属性 Text 的值

    <Data Text=\"Text for translation\">

    //data/@text

  7. 导入 <机器翻译:translation> 元素及其子元素,排除元素 <机器翻译:略过>

    //mT:translation/descendant-or-self::*[not(ancestor-or-self::mT:ignore)]

  8. 排除所有属性 translate='false' 的元素

    //*[not(@translate='false')]

  9. 排除属性 translate='false' 的“lis”元素

    (<lis translate=\"false\">Do not translate)

    //*[not(self::lis[@translate='false'])]

  10. 排除属性 translate='false' 的“lis”元素及其子元素

    (<lis translate="false"><p>Do not translate)

    //*[not(ancestor-or-self::lis[@translate='false'])]

  11. 排除所有包含“link”的元素

    (<menu1link><tmenu41link>)

    //*[not(contains(name(),'link'))]

  12. 如果元素本身或其父元素具有包含“link”的“lis”属性,则排除该元素

    (<ele lis=menu1link>, <mon lis=tmenu41link>)

    //*[not(ancestor-or-self::node()[contains(@lis, 'link')])]

  13. 如果父元素 LANG 具有 year 2015 的“updated”属性,则导入元素 PT

    (<LANG updated="20150213T121526"><PT>'

    //LANG[contains(@updated,'2015')]/PT

  14. 仅在元素“Description”和“Name”不是元素“Definitions”或“Types”的后代时才导入它们

    ://*[not(ancestor-or-self::*[(name()='Definitions') or (name()='Types')])]/*

    [(name()='Description') or (name()='Name')]

  15. 带有命名空间的XML

    <root xmlns:xhtml="http://www.w3.org/1999/xhtml"><Child><Text>translate this</Text></Child>

    • 导入 <子> 中的所有元素:

      //*[local-name()='Child']//*

    • 仅导入 <子> 中的元素 <Text>

      //*[local-name()='Child']/*[local-name()='Text']

    • 导入元素 <CONTRACT> 下的所有元素,如果 <CONTRACT> 中的属性 <类别> 的值为 <ORIGINAL>

      ://*[local-name()='CONTRACT' and @CATEGORY='ORIGINAL']//*

  16. 带有命名空间和属性的XML <root> xmlns:xhtml=\"http://www.w3.org/1999/xhtml\"<Child translate='1'>翻译此内容</Child>

    • 如果属性translate为1,则导入元素 <子>//*[local-name()=\"Child\"][@*[local-name()='translate']='1']

    • 导入所有属性translate=true的元素://*[@*[local-name()='translate']='true']

  17. 带有命名空间的XML。导入元素译文,来自元素 tu,但当属性ID包含'img'或'extra'时不导入:

    1. 文件示例:

      <tu id=\"pages|content|extra\"><ori xml:lang=\"en\">Course one</ori><target xml:lang=\"lang\">Course one</target></tu>

    2. XPATH示例:

      //*[local-name()='tu' and not(contains(@id,'img') or contains(@id,'extra'))]/*[local-name()='target']

  18. 导入除 <备注><lis> 之外的所有元素,除非是 <lis translate=\"true\"> 及其后代:

    //*[count(ancestor-or-self::node()[(name()='lis' and (not(@translate='true')) ) or name()='comment'])=0]

  19. 导入所有元素,但 <备注> 以及具有属性 <... attribute2=\"Do not translate\"> 的元素及其后代除外:

    //*[count(ancestor-or-self::node()[(@attribute2='Do not translate') or name()='comment'])=0]

  20. 导入属性 varNameglossName 的值,但仅限其祖先具有属性 attribute1='translate'attribute1='编辑' 的情况:

    //*[(self::node()[@attribute1='translate' or @attribute1='edit'])]//@*[local-name()='varName' or local-name()='glossName']

  21. 导入所有元素和属性,但具有属性 Name= BackMenuTime 的元素除外:

    //*[not(ancestor-or-self::node()[@Name='Back' or @Name='Menu' or @Name='Time'])] | //@*[not(ancestor-or-self::node()[@Name='Back' or @Name='Menu' or @Name='Time'])]

    在这种情况下,最好导入所有内容并锁定不需要导入的属性。一旦段落被“已锁定”,请在编辑器中将原文/源语复制到译文,以便将原文传输到翻译中。

    1. 锁定所有 Name 属性值为 BackMenuTime 的元素及其后代:

      //*[@Name='Back' or @Name='Menu' or @Name='Time']/descendant-or-self::*

    2. 锁定 Name 值为 BackMenuTime 的所有属性及其后代

      //*[@Name='Back' or @Name='Menu' or @Name='Time']//@*

一些外部 示例

上下文注释

上下文注释可以导入到译文段落中。

此示例中有三个示例:

<?xml version-"1.0" encoding="utf-8"?>
<root>
<element context1=\"Note in attribute of parentElement 1 - select ../@context1\">
<field context2=\"Note in attribute 1 - select @context2\" >for translation1</field>
<context3>Note in element 1 - select ../context3</context3>
</element>

<element context1=\"父元素 2 的属性中的注释\">
<field context2=\"属性 2 中的注释\">用于翻译2</field>
<context3>元素 2 中的注释</context3>
</element>

</root>
  • 父元素中的属性(上下文 1):../@context1

  • 自身元素中的属性(上下文 2):@context2

  • 同级元素(上下文 3):../context3

如果导入 元素与属性: //*,上下文注释的内容也将被导入到原文/源语段落中。在“元素与属性”中从常规导入中排除上下文注释元素/属性。

这篇文章有帮助吗?

Sorry about that! In what way was it not helpful?

The article didn’t address my problem.
I couldn’t understand the article.
The feature doesn’t do what I need.
Other reason.

Note that feedback is provided anonymously so we aren't able to reply to questions.
If you'd like to ask a question, submit a request to our Support team.
Thank you for your feedback.