0

I have a Translation Memory which is essentially an XML file based on Translation Memory eXchange format specifications and I am trying to find a specific translation unit for editing. This is an example of the structure:

 <?xml version="1.0" encoding="utf-8"?>
    <tmx version="1.4">
    <header creationtool="xxx" .... />
    <body>
    <tu tuid="1">
      <tuv xml:lang="en-US">
        <seg>sample source</seg>
      </tuv>
      <tuv xml:lang="de-DE">
        <seg>sample translation</seg>
      </tuv>
    </tu>
    <tu tuid="2">
      <tuv xml:lang="en-US">
        <seg>Address</seg>
      </tuv>
      <tuv xml:lang="de-DE">
        <seg>Adresse</seg>
      </tuv>
    </tu>
    .....
    </body>
    </tmx>

What I want is to be able to find all the translation units (tu) that have a specific source translation and a specific target translation. So for example I want to find all translation units where the xml language attribute value is "en-US" and the seg element value is "sample source" and the xml language attribute value is "de-DE" and its seg element value is "sample translation". I want to find

<tu tuid="18">
      <tuv xml:lang="en-AU">
        <seg>sample source</seg>
      </tuv>
      <tuv xml:lang="de-DE">
        <seg>sample translation</seg>
      </tuv>
 </tu>

It is possible as well there is more than one translation unit (tu) that fits the criteria - that is there is possibly duplicates in the translation memory.

I have tried to get a collection I could iterate through e.g.

XElement root = XElement.Load(@"sample.tmx");
        IEnumerable<XElement> translationUnits =
         from el in root.Elements("tu")
         where
             (from tuv in el.Elements("tuv")
              where
                  (string)tuv.Attribute(XNamespace.Xml + "lang") == "en-US" &&
                  (string)tuv.Element("seg") == "sample source"
              select tuv)
             .Any()
         select el;
                foreach (XElement el in translationUnits)
                    Console.WriteLine((string)el.Attribute("tuid"));

However I am obviously doing something wrong however I think I am on the right track. Once I find the collection I then want to update the target translation.

smuzoen
  • 199
  • 14

1 Answers1

2

The way I eventually solved this for future reference is using XmlDocument

XmlDocument document = new XmlDocument();
document.Load(this.fileName);
string nodeSelect = "/tmx/body/tu/tuv[lang('" + this.sourceLanguage + "') and seg = '" + this.originalSourceText + "']";            
XmlNodeList nodes = document.DocumentElement.SelectNodes(nodeSelect);
foreach (XmlNode node in nodes) {
   XmlNode parent = node.ParentNode;
   foreach (XmlNode translationNode in parent) {
     string searchNode = "*[lang('" + this.targetLanguage + "') and //seg = '" + this.originalTranslationText + "']";
     XmlNode test = translationNode.SelectSingleNode(searchNode);
     if (test != null) {
          if (test.InnerText.Equals(this.originalTranslationText, StringComparison.Ordinal)) {
              test.InnerText = this.newTranslation;
          }
     }
  }
} 
smuzoen
  • 199
  • 14