File Import settings

XPath

Introduction

XPath stands for XML Path Language. It can be used to navigate through elements and attributes in an XML document. If you are new to XPATH, please see this XPath Tutorial first and study the official XPATH documentation.

A subset of XPath 1.0 is accepted with the following limitations:

  • Axis in step
    • Supported: ancestor, ancestor-or-self, attribute, child, descendant, descendant-or-self
    • Not supported: following, preceding, following-sibling, preceding-sibling, namespace
  • Predicate
    • Supported: conditions on current node or ancestor nodes and its properties (attributes, namespaces)
    • Not supported (for example): position number, axis child::, descendant, descendant-or-self, following::, preceding::, following-sibling::, preceding-sibling::, function last()

Basic Rules

  • Use / and // in paths
  • Use single quotes ‘ ‘ in names
  • Use a pipe | for joining the requests
  • Names are case-sensitive: <Body> is different from <body>

Examples

There are online examples which can help you to understand XPath better.

For example, files in the attachment include XPath Example 1 and XPath example 2 (with namespace).

  1. Import all elements and all attributes

    //* | //@*

  2. Import all elements and the value of attribute1 (<elem1 attribute1="translate" attribute2="Do not translate"/>)

    //* | //@attribute1

  3. Import all descendants of Child elements

    //Child//*

  4. Import an element lis and its descendants only if the attribute translate='true' (<lis translate="true">translate this</lis><lis translate="false">do not translate this</lis>)

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

  5. Import all elements and descendants if the element's attribute is translate='true'

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

  6. Import value of the attribute Text in element Data <Data Text="Text for translation">

    //data/@text

  7. Import the <mT:translation> element and its descendants, except element <mT:ignore>

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

  8. Exclude all elements with the attribute translate='false'
    //*[not(@translate='false')]
  9. Exclude elements 'lis' with the attribute translate='false' (<lis translate="false">Do not translate)

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

  10. Exclude elements 'lis' with the attribute translate='false' and their descentants (<lis translate="false"><p>Do not translate)

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

  11. Exclude all elements containing 'link' (<menu1link>, <tmenu41link>)

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

  12. Exclude all elements if they or their ancestor have an attribute 'lis' containing 'link' (<ele lis=menu1link>, <mon lis=tmenu41link>)

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

  13. import elements PT if their parent LANG has the attribute 'updated' with year 2015 (<LANG updated="20150213T121526"><PT>)'

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

  14. Import elements 'Description' and 'Name' only if they are not descendants of elements 'Definitions' or 'Types':

    //*[not(ancestor-or-self::*[(name()='Definitions') or (name()='Types')])]/*[(name()='Description') or (name()='Name')]
  15. XML with namespace <root xmlns:xhtml="http://www.w3.org/1999/xhtml"> <Child><Text>translate this</Text></Child>
    • import all elements in <Child>:

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

    • import only elements <Text> in <Child>:

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

    • import all elements under element <CONTRACT>, if attribute <CATEGORY> in <CONTRACT> has the value <ORIGINAL>:

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

  16. XML with namespace and attributes <root xmlns:xhtml="http://www.w3.org/1999/xhtml"> <Child translate='1'>translate this</Child>
    • import element <Child> if the attribute translate is 1:

      //*[local-name()="Child"][@*[local-name()='translate']='1']

    • import all elements with the attribute translate=true:

      //*[@*[local-name()='translate']='true']

  17. XML with namespace. Import the element target from element tu, but not when the attribute id contains 'img' or 'extra':
    • File example:

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

    • XPATH example:

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

  18. Import all elements except <comment> and <lis> unless <lis translate="true"> and their descendants:

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

  19. Import all elements except <comment> and except elements with attribute <... attribute2="Do not translate"> and their descendants:

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

  20. Import values of attributes 'varName' and 'glossName' but only if their ancestors have attribute attribute1='translate' or attribute1='edit':

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

  21. Import all elements and attributes except elements with the attribute Name= Back, Menu, or Time

    //*[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'])]
In this case it could be better to import all and lock the atributes you don’t want to import (see following points.). Once the segments are Locked, you have to copy source to target in Editor in order to have original transferred to translation.
  • Lock all elements with the attribute Name with values: Back, Menu, or Time and their descendants

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

  • Lock all attributes of Name with values: Back, Menu, or Time and their descendants

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

Multilingual XML with Namespace

If your multilingual XML contains namespace,  the xpath could be the following: 

  • //*[local-name()='trans-unit']
  • *[local-name()='source']
  • *[local-name()='target']

Context Note

it is possible to import context notes to translated segments. There are three examples in this Sample:

  • Attribute in parent element (context 1): ../@context1
  • Attribute in self element (context2): @context2
  • Sibling element (context 3): ../context3

Be aware, that if you import

  • Elements&Attributes: //*

the content of the context note will be imported to source segments as well. You will have to exclude the context note elements/attributes from general import in Elements&Attributes.

 

Samples attached: