Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read specific attribute from TXM (XML) tag using PHP

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading