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.
AI chatbots can be very effective at generating and verifying Xpath.
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:
-
Import all elements and all attributes
//* | //@*
-
Import all elements and the value of attribute1
(<elem1 attribute1="translate" attribute2="Do not translate"/>)
//* | //@attribute1
-
Import all descendants of Child elements
//Child//*
-
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::*
-
Import all elements and descendants if the element's attribute is translate='true'
//*[@translate='true']/descendant-or-self::*
-
Import value of the attribute Text in element Data
<Data Text="Text for translation">
//data/@text
-
Import the
<mT:translation>
element and its descendants, except element<mT:ignore>
//mT:translation/descendant-or-self::*[not(ancestor-or-self::mT:ignore)]
-
Exclude all elements with the attribute translate='false'
//*[not(@translate='false')]
-
Exclude elements 'lis' with the attribute translate='false'
(<lis translate="false">Do not translate)
//*[not(self::lis[@translate='false'])]
-
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'])]
-
Exclude all elements containing 'link'
(<menu1link><tmenu41link>)
//*[not(contains(name(),'link'))]
-
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')])]
-
Import elements PT if their parent LANG has the attribute 'updated' with year 2015
(<LANG updated="20150213T121526"><PT>'
//LANG[contains(@updated,'2015')]/PT
-
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')]
-
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']//*
-
-
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']
-
-
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']
-
-
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]
-
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]
-
Import values of attributes
varName
andglossName
but only if their ancestors have attributeattribute1='translate'
orattribute1='edit'
://*[(self::node()[@attribute1='translate' or @attribute1='edit'])]//@*[local-name()='varName' or local-name()='glossName']
-
Import all elements and attributes except elements with the attribute
Name= Back
,Menu
, orTime
://*[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.
-
Lock all elements with the attribute
Name
with values:Back
,Menu
, orTime
and their descendants://*[@Name='Back' or @Name='Menu' or @Name='Time']/descendant-or-self::*
-
Lock all attributes of
Name
with values:Back
,Menu
, orTime
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.