从段落中删除内联图像

Removing inline image from paragraph

我遇到了 XSLT 问题,完全被卡住了。

我的情况如下,收到的是word文档。我必须将其转换为我们内部的 XML 格式。在这种格式中,图像 与段落分开。

我尝试了很多方法,例如每个循环、模板、使用 helpercode,但我认为我对 XSLT 的了解仅限于解决问题。

简而言之,我收到的 Wordxml 如下

<w:document>
    <w:p>
        <w:r>
            <w:t>sometext</w:t>
        </w:r>
        <w:r>
            <w:drawing></w:drawing>
        </w:r>
        <w:r>
            <w:t>anothertext</w:t>
        </w:r>
    </w:p>
</w:document>

我正在尝试创建以下任一结果。

选项 1:

<w:document>
    <w:p>
        <w:r>
            <w:t>sometext</w:t>
        </w:r>
    </w:p>
    <w:drawing></w:drawing>
    <w:p>
        <w:r>
            <w:t>anothertext</w:t>
        </w:r>
    </w:p>
</w:document>

选项2:

<w:document>
    <w:p>
        <w:r>
            <w:t>sometext</w:t>
        </w:r>
        <w:r>
            <w:t>anothertext</w:t>
        </w:r>
    </w:p>
    <w:drawing></w:drawing>
</w:document>

试试这个:

<xsl:template match="w:p[w:r/w:drawing]">
  <xsl:copy>
    <xsl:apply-templates select="*[not(w:drawing)]"/>
  </xsl:copy>
  <xsl:apply-templates select="w:r/w:drawing"/>
</xsl:template>

我不确定这是否涵盖所有可能性,但它应该按照选项 2 和给定的示例输出。

这是选项 1 的解决方案:

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

<xsl:template match="w:p">                     <!-- remove outter w:p -->
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="w:r[w:drawing]">          <!-- move up w:drawing one level -->
  <xsl:copy-of select="*" />
</xsl:template>

<xsl:template match="w:r">                     <!-- encapsulate w:r in w:p and copy it -->
  <w:p>
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </w:p>
</xsl:template>