Here is an example TU (translation unit) from a TMX file:
<tu creationdate="20080812T111221Z" creationid="Cheeseus" changedate="20190825T065920Z" changeid="Cheeseus" usagecount="0" tuid="2">
<tuv xml:lang="BG">
<seg>ПАРТНЬОРИ</seg>
</tuv>
<tuv xml:lang="EN-GB">
<seg>PARTNERS</seg>
</tuv>
</tu>
I read the file this way:
$xmlStr = file_get_contents($uploadedFile);
$xmlObj = simplexml_load_string($xmlStr);
$TUs = $xmlObj->body->tu;
foreach($TUs as $TU) {
if(isset($TU->tuv[0]->seg)) {
$sourceText = $TU->tuv[0]->seg->asXML();
$targetText = $TU->tuv[1]->seg->asXML();
}
}
I am now trying also read the xml:lang attribute of each tuv. I thought I’d be able to do this the same way I access the attributes of the parent tu, e.g. $TU['creationdate']. But the same approach doesn’t work.
I’ve tried:
var_dump($TU->tuv[0]['xml:lang']) /* result: null */
var_dump($TU->tuv['xml:lang']); /* result: null */
var_dump($TU->tuv[0]['attributes']['xml:lang']); /* result: Warning: Trying to access array offset on value of type null... */
var_dump($TU->tuv[0]['@attributes']['xml:lang']); /* result: Warning: Trying to access array offset on value of type null... */
and a few others that break the script completely. I also tried accessing this attribute without the "xml:" part – to the same effect.
I hope you can point me in the right direction.
>Solution :
To read attributes that are part of a namespace, you’ll often need to use the attributes() method and specify the namespace URI. Since xml: is a predefined namespace in XML, its URI is http://www.w3.org/XML/1998/namespace:
$TU->tuv[0]->attributes('http://www.w3.org/XML/1998/namespace')->lang;
but you can pass ('xml', true) as arguments, instead:
$TU->tuv[0]->attributes('xml', true)->lang;
So working code should be like this (BTW: I am not sure what you called ->asXml() for when reading node value, so I removed that):
$xmlStr = file_get_contents($uploadedFile);
$xmlObj = simplexml_load_string($xmlStr);
$TUs = $xmlObj->body->tu;
foreach($TUs as $TU) {
if (isset($TU->tuv[0]->seg)) {
$sourceText = $TU->tuv[0]->seg;
$targetText = $TU->tuv[1]->seg;
$sourceLang = (string)$TU->tuv[0]->attributes('xml', true)->lang;
$targetLang = (string)$TU->tuv[1]->attributes('xml', true)->lang;
echo "Source Text: {$sourceText}, Source Language: {$sourceLang}\n";
echo "Target Text: {$targetText}, Target Language: {$targetLang}\n";
}
}
would produce:
Source Text: ПАРТНЬОРИ, Source Language: BG
Target Text: PARTNERS, Target Language: EN-GB