使用XSLT 1.0将XML转换为WordML-用wordML格式标签替换xml内容中的html标签 [英] XML to WordML using XSLT 1.0 - replace html tags within xml content with wordML formatting tags

查看:176
本文介绍了使用XSLT 1.0将XML转换为WordML-用wordML格式标签替换xml内容中的html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个XML文件创建一个WordML文档,该文件的元素有时包含html格式的文本.

I am creating a WordML document from an xml file whose elements sometimes contain html-formatted text.

<w:p>
  <w:r>
    <w:t> html formatted content is in here taken from xml file! </w:t>
  </w:r>
</w:p>

这是我的模板的设置方式.我有一个递归调用模板功能,可以对源xml内容进行文本替换.当遇到"<b>"标记时,我在CDATA中输出一个包含"</w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>"的字符串,以关闭当前运行并启动启用了粗体格式的新运行.当到达"</b>"标记时,它将替换为以下CDATA字符串"</w:t></w:r><w:r><w:t>".

This is how my templates are sort of set up. I have a recursive call-template function that does text replacement against the source xml content. When it comes across a "<b>" tag, I output a string in CDATA containing "</w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>" to close the current run and start up a new run with bold formatting enabled. when it gets to a "</b>" tag, it replaces it with the following CDATA string "</w:t></w:r><w:r><w:t>".

我想做的是使用XSL关闭运行标签并开始新的运行,而无需使用CDATA字符串插入.这可能吗?

What I'd like to do is use XSL to close the run tag and start a new run without using CDATA string inserts. Is this possible?

推荐答案

使用WordML十分棘手.使用XSLT将任意XML转换为WordML时,一个技巧是不要担心在处理块时文本会运行,而是要创建一个直接匹配text()节点的模板,然后在此处运行文本.事实证明,Word不在乎是否嵌套文本运行,这使问题更容易解决.

Working with WordML is tricky. One tip when converting arbitrary XML to WordML using XSLT is to not worry about the text runs when processing blocks, but to instead create a template that matches text() nodes directly, and create the text runs there. It turns out that Word doesn't care if you nest text runs, which makes the problem much easier to solve.

   <xsl:template match="text()" priority="1">
         <w:r>
            <w:t>
               <xsl:value-of select="."/>
            </w:t>
         </w:r> 
   </xsl:template>

   <xsl:template match="@*|node()">
       <xsl:apply-templates select="@*|node()"/>
   </xsl:template>

   <xsl:template match="para">
      <w:p>
         <xsl:apply-templates select="text() | *" />
      </w:p>
   </xsl:template>

   <xsl:template match="b">
      <w:r>
         <w:rPr>
            <w:b />
         </w:rPr>
         <w:t><xsl:apply-templates /></w:t>
      </w:r>
   </xsl:template>

这避免了将XSLT直接插入为转义文本的不良XSLT技术.随着嵌套文本的运行,您最终将得到粗体标签,但是正如我所说的,Word一点也不在乎.如果使用此技术,则需要注意不要将模板应用于段落之间的空白处,因为它将触发文本模板并创建上下文外运行.

This avoids the bad XSLT technique of inserting tags directly as escaped text. You'll end up with the bold tag as a nested text run, but as I said, Word couldn't care less. If you use this technique, you'll need to be careful to not apply templates to the empty space between paragraphs, since it will trigger the text template and create an out-of-context run.

这篇关于使用XSLT 1.0将XML转换为WordML-用wordML格式标签替换xml内容中的html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆