0

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?

Community
  • 1
  • 1
Sarah Kemp
  • 2,670
  • 3
  • 21
  • 29
  • "to pull this data from the larger template at the same time" => what do you mean ? Can you give more precision ? "P.S. Should I be asking this on the TBS forum instead?" => as you wish, both are ok. – Skrol29 Jan 30 '13 at 15:31
  • Sorry I was not clear, I have added some pseudo code I hope will explain better. – Sarah Kemp Jan 30 '13 at 16:19

1 Answers1

1

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";
   }
}
Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • Thank you! I was able to add a unique block definition to each of my subtemplates and use the $TBS->GetBlockSource method easily. Now my subtemplates are each ~100KB less than they were, and my test cases run about 3 times faster now. Thank you very much! – Sarah Kemp Jan 31 '13 at 16:58