0

I have just one section of my XML that I want to perform some operations in the case that there is a field missing.

In the trans-unit there is a source tag as well as a target tag.

However there are parts with missing target tags and values -

I would like to be able to check for these. If the target field is missing, I would like to get the source's value, translate it, and add the target tag with the translated text as its value.

(I will likely use translate-shell for the translation.)

The structure looks like this

<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd">
<file original="WabiTime/en.lproj/Localizable.strings" source-language="en" target-language="fr" datatype="plaintext">
    <header>
      <tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="13.3" build-num="13E113"/>
    </header>
    <body>
      <trans-unit id="%@ complete" xml:space="preserve">
        <source>%@ complete</source>
        <target>complété à %@</target>
        <note/>
      </trans-unit>
      <trans-unit id="%lld Completed" xml:space="preserve">
        <source>%lld Completed</source>
        <target>%lld complète</target>
        <note/>
      </trans-unit>
      <trans-unit id="(No project): %@" xml:space="preserve">
        <source>(No project): %@</source>
        <target>(aucun projet): %@</target>
        <note/>
      </trans-unit>
      <trans-unit id="A time limit should be greater than or equal to 1 minute" xml:space="preserve">
        <source>A time limit should be greater than or equal to 1 minute</source>
        <target>Une limite de temps doit être supérieure ou égale à 1 minute</target>
        <note>alert body</note>
      </trans-unit>
      <trans-unit id="About WabiTime" xml:space="preserve">
        <source>About WabiTime</source>
        <target>A propos WabiTime</target>
        <note>menu item</note>
      </trans-unit>
      <trans-unit id="Active tint color:" xml:space="preserve">
        <source>Active tint color:</source>
        <note/>
      </trans-unit>
</body>
 </file>
</xliff>

If I use xml el -v fr.xliff then I can see the path I want shown as;

xliff/file[@original='WabiTime/en.lproj/Localizable.strings' and @source-language='en' and @target-language='fr' and @datatype='plaintext']

However I can't seem to figure out how to work with this.

How do I do my if check and edit in the new field?

Joey Slomowitz
  • 179
  • 1
  • 12
  • Closing this question on the grounds that it is namespace-related is a mistake. – urznow Apr 04 '22 at 07:20
  • 1
    Unfortunately your question is closed at the moment so I'll have to answer in a comment. To list `source` texts of `trans-unit` elements not having a `target` you can say `xmlstarlet select -T -t -m '//trans-unit[not(target)]' -v 'source' -n file.xml`. To add `target`s instead: `xmlstarlet edit --pf -a '//trans-unit[not(target)]/source' -t 'elem' -n 'target' -v 'TODO' file.xml > newfile.xml`. User's guide is at [xmlstar.sourceforge.net/doc/UG/](http://xmlstar.sourceforge.net/doc/UG/). – urznow Apr 04 '22 at 08:49
  • thanks so much @urcodebetterznow, I can't seem to get that list with `xmlstarlet select -T -t -m '//trans-unit[not(target)]' -v 'source' -n file.xml`. When I hit 'enter', nothing happens. Is there something wrong with the path? – Joey Slomowitz Apr 04 '22 at 22:26
  • 1
    Oops, I missed the first line in the input. This should work: `xmlstarlet select -T -t -m '//_:trans-unit[not(_:target)]' -v '_:source' -n file.xml` and `xmlstarlet edit --pf -a '//_:trans-unit[not(_:target)]/_:source' -t 'elem' -n 'target' -v 'TODO' file.xml > newfile.xml`, using the default namespace [shortcut](http://xmlstar.sourceforge.net/doc/UG/ch05.html#idm47989546158480). – urznow Apr 05 '22 at 07:33
  • Thanks @urcodebetterznow what you've posted above does get me the fields I'm after. However, how would I be able to only get the trans-unit fields that are inside the following field with this specific namespace? `file original="WabiTime/en.lproj/Localizable.strings" source-language="en" target-language="fr" datatype="plaintext"` ? I've tried `xml sel -t -v "//*[name()='WabiTime/en.lproj/Localizable.strings']" fr.xliff` but I don't get anything after hitting enter. – Joey Slomowitz Apr 06 '22 at 15:36
  • Replacing `//_:trans-unit` from my last comment with `//_:file[@original="WabiTime/en.lproj/Localizable.strings"][@target-language="fr"]//_:trans-unit` (2 places) should work. The predicates `[…]` work as ANDed selectors. BTW, the key="value"s inside `` are attributes, not a namespace. – urznow Apr 06 '22 at 17:17

0 Answers0