Question

As a follow up to my previous question, I am trying to really simplify several xml templates extracted from .docx files created in Word 2010 and used as subtemplates in OpenTBS in order to speed up my document creation. The subtemplates contain an mc:AlternateContent block which is all I use in my main template, like this:

[LineItem.template;block=w:r;file='templates/[val].xml';getpart=(mc:AlternateContent)]

I noticed that multiple (20) large (>100KB) subtemplates can seriously affect OpenTBS' speed, so I would like to eliminate the code I am not using (outside of mc:AlternateContent). I am already planning on doing other processing on my templates through TBS and caching the simplified version, so it would be great if I could use OpenTBS' getpart functionality to pull this data from the larger template at the same time. Is this possible?

For instance, to be able to fit this (pseudo-) code into my processing:

foreach($templates as $template){ //loop through xml templates
    $TBS->LoadTemplate($template);
    $simpleTemplate = $template->getpart('mc:AlternateContent'); 
    /* 
    / simpleTemplate now holds all the xml inside the mc:AlternateContent tags
    / (everything that would have been included in my template had I used attribute
    / getpart=(mc:AlternateContent) in my file inclusion) 
    */
    $simpleTemplate->save('simple/'.$template);
}

P.S. Should I be asking this on the TBS forum instead?

Was it helpful?

Solution

Here is two solutions to extract an XML element from set of files (which are TBS sub-templates).

1)

TBS has a documented method that enables you to get the source code of a TBS block, with or without the TBS fields that define the block.

See $TBS->GetBlockSource()

You can use this method if you already have a TBS block for that part, or if it is possible for you to create a dedicated block in your sub-template.

2)

Otherwise, you can use an undocumented class clsTbsXmlLoc provided with OpenTBS:

foreach ($templates as $template) {
   $contents = file_get_contents($template);
   $x = clsTbsXmlLoc::FindElement($contents, 'mc:AlternateContent', 0);
   if ($x) {
      $src = $x->GetSrc();
      // use $x->GetInnerSrc() in order to get the content of <mc:AlternateContent>
      // but wihtout the <mc:AlternateContent> tags.
      file_put_contents('simple/'.$template);
   } else {
      echo "Element no found in sub-template $template";
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top