将斜体XML标签转换为WordML标签

时间:2012-09-18 01:01:43

标签: xml xslt wordml

我只需要使用此表单将XML文档转换为WordML文档(如果可以简单地调用它!)(无需处理说明):

<body>
    <p>
        <r>This is the <italic>standard</italic> text run.</r> 
    </p>
</body>

根据WordML文档,转换后的XML应该如下所示:

<w:body>
    <w:p>
        <w:r>
            <w:t>This is the </w:t> 
        </w:r>
    </w:p>
    <w:p>
        <w:pPr>
            <w:i/>
        </w:pPr>
        <w:r>
            <w:t>standard</w:t> 
        </w:r>
    </w:p>
    <w:p>
        <w:r>
            <w:t> text run.</w:t> 
        </w:r>
    </w:p>
</w:body>

我应该如何创建XSLT转换以正确处理Italic标签?? ..

2 个答案:

答案 0 :(得分:1)

对于像提供的示例一样简单的输入,以下样式表将起作用。使用经过修改的identity transform,其中包含<italics>r/text()

的专用模板
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
        xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
    <xsl:output indent="yes"/>

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

    <xsl:template match="body">
        <w:body>
            <xsl:apply-templates/>
        </w:body>
    </xsl:template>

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

    <xsl:template match="r/italic">
        <w:p>
            <w:pPr>
                <w:i/>
            </w:pPr>
            <w:r>
                <w:t><xsl:value-of select="."/></w:t> 
            </w:r>
        </w:p>
    </xsl:template>  

</xsl:stylesheet>

答案 1 :(得分:1)

此转化

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="some:w" exclude-result-prefixes="w">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="body">
  <w:body>
   <xsl:apply-templates/>
  </w:body>
 </xsl:template>

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

 <xsl:template match="p/r/italic/text()">
    <w:p>
        <w:pPr>
            <w:i/>
        </w:pPr>
        <w:r>
            <w:t><xsl:value-of select="."/></w:t>
        </w:r>
    </w:p>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的XML文档时:

<body>
    <p>
        <r>This is the <italic>standard</italic> text run.</r>
    </p>
</body>

会产生想要的正确结果:

<w:body xmlns:w="some:w">
   <w:p>
      <w:r>
         <w:t>This is the </w:t>
      </w:r>
   </w:p>
   <w:p>
      <w:pPr>
         <w:i/>
      </w:pPr>
      <w:r>
         <w:t>standard</w:t>
      </w:r>
   </w:p>
   <w:p>
      <w:r>
         <w:t> text run.</w:t>
      </w:r>
   </w:p>
</w:body>
相关问题