【问题标题】:Simplifying subtemplates that use 'getpart' parameter to speed up OpenTBS简化使用'getpart'参数来加速OpenTBS的子模板
【发布时间】:2013-01-30 00:25:37
【问题描述】:

作为我previous question 的后续行动,我正在尝试真正简化从在 Word 2010 中创建的.docx 文件中提取的几个xml 模板,并在 OpenTBS 中用作子模板,以加快我的文档创建速度。子模板包含一个 mc:AlternateContent 块,这是我在主模板中使用的全部内容,如下所示:

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

我注意到多个 (20) 个大 (>100KB) 子模板会严重影响 OpenTBS 的速度,因此我想删除我不使用的代码(在 mc:AlternateContent 之外)。我已经计划通过 TBS 对我的模板进行其他处理并缓存简化版本,所以如果我可以使用 OpenTBS 的 getpart 功能同时从更大的模板中提取这些数据,那就太好了。这可能吗?

例如,为了能够将此(伪)代码放入我的处理中:

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);
}

附注我应该在the TBS forum 上问这个吗?

【问题讨论】:

  • "同时从更大的模板中提取这些数据" => 你是什么意思?你能给出更精确的吗? “P.S.我应该在TBS论坛上问这个吗?” => 如你所愿,两者都可以。
  • 对不起,我没有说清楚,我添加了一些伪代码,希望能解释得更好。

标签: opentbs tinybutstrong


【解决方案1】:

这是从一组文件(TBS 子模板)中提取 XML 元素的两种解决方案。

1)

TBS 有一个记录方法,使您能够获取 TBS 块的源代码,无论是否包含定义块的 TBS 字段。

$TBS->GetBlockSource()

如果您已经有该部分的 TBS 块,或者您可以在子模板中创建专用块,则可以使用此方法。

2)

否则,您可以使用 OpenTBS 提供的未记录类 clsTbsXmlLoc

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";
   }
}

【讨论】:

  • 谢谢!我能够为每个子模板添加唯一的块定义,并轻松使用 $TBS->GetBlockSource 方法。现在我的子模板每个都比原来少了约 100KB,我的测试用例现在运行速度快了大约 3 倍。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多