Import Utilities

XPath

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

A subset of XPath 1.0 is supported 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

XPath Example 1 and XPath example 2 (with namespace) are example files for the following:

  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':

    1. File example:

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

    2. 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 may be better to import all and lock the attributes not required for import. Once segments are Locked, copy source to target in the editor in order to have the original transferred to translation.

    1. 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::*

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

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

Some external examples.

Context Note

Context notes can be imported into translated segments.

There are three examples in this sample:

<?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="Note in attribute of parentElement 2">
<field context2="Note in attribute 2">for translation2</field>
<context3>Note in element 2</context3>
</element>

</root>
  • Attribute in parent element (context 1): ../@context1

  • Attribute in self element (context2): @context2

  • Sibling element (context 3): ../context3

If importing Elements&Attributes: //* the content of the context note will be imported to source segments as well. Exclude the context note elements/attributes from general import in Elements&Attributes.

Was this article helpful?

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 you can leave a public comment below or Submit a request to our Support team.
Thank you for your feedback.

Comments

0 comments

Article is closed for comments.