2
votes

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

2

2 Answers

1
votes

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

For input as simple as the example provided, the following stylesheet would work. Using a modified identity transform with specialized templates for the <italics> and 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>