Search code examples
xmlxsltwordml

Convert Italic XML Tags into WordML Tags


I simply need to transforma XML document into a WordML document (if it is possible to call it simple!) with this form (without processing instructions):

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

The transformed XML should look like this, as per an WordML document:

<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>

How should i create the XSLT transformation for properly handle the Italic tags??..


Solution

  • This transformation:

    <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>
    

    when applied on the provided XML document:

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

    produces the wanted, correct result:

    <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>