Insert formatted text

Nov 20, 2009 at 8:32 AM

Hello

I need to insert formatted text into a (rich text) contentcontrol. I take the formatted text from an existing word document (docx) - it looks like this:

<w:r>
        <w:t xml:space="preserve">Hallo Welt. Ich bin ein </w:t>
      </w:r>
      <w:r w:rsidRPr="00B669B4">
        <w:rPr>
          <w:sz w:val="48"/>
          <w:szCs w:val="48"/>
        </w:rPr>
        <w:t>Content</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve"> der </w:t>
      </w:r>
      <w:r w:rsidRPr="00B669B4">
        <w:rPr>
          <w:b/>
        </w:rPr>
        <w:t>mal einfac</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve">h so </w:t>
      </w:r>
      <w:r w:rsidRPr="00B669B4">
        <w:rPr>
          <w:highlight w:val="red"/>
        </w:rPr>
        <w:t>eingegeben</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve"> wurde…</w:t>
      </w:r>

What happens is, that fleXdoc seems to remove all formattings. The result in the generated document looks like this:

<w:sdtContent>
          <w:r>
            <w:t>Hallo Welt. Ich bin ein Content der mal einfach so eingegeben wurde…</w:t>
          </w:r>
        </w:sdtContent>

So this is the right text - but all formattings are gone.

Is there a way to preserve the formattings and just insert that stuff right as it is?

Thanks

Dani

Nov 20, 2009 at 8:37 AM

More Information... my input-xml looks like this:

<sample
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <formattedText>
      <w:r>
        <w:t xml:space="preserve">Hallo Welt. Ich bin ein </w:t>
      </w:r>
      <w:r w:rsidRPr="00B669B4">
        <w:rPr>
          <w:sz w:val="48"/>
          <w:szCs w:val="48"/>
        </w:rPr>
        <w:t>Content</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve"> der </w:t>
      </w:r>
      <w:r w:rsidRPr="00B669B4">
        <w:rPr>
          <w:b/>
        </w:rPr>
        <w:t>mal einfac</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve">h so </w:t>
      </w:r>
      <w:r w:rsidRPr="00B669B4">
        <w:rPr>
          <w:highlight w:val="red"/>
        </w:rPr>
        <w:t>eingegeben</w:t>
      </w:r>
      <w:r>
        <w:t xml:space="preserve"> wurde…</w:t>
      </w:r>
  </formattedText>
</sample>

In the template I've a valueof-tag with path attribute "sample/formattedText"

Coordinator
Nov 20, 2009 at 9:53 AM

Hello Dani,

Try a CDATA-section, like this:

<sample xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <formattedText>
        <![CDATA[ <xml /> ]]>
    </formattedText>
</sample>

Also, the value of your xpath is an empty node/empty string, because the selected element itself contains no text. It does have childnodes, but those are not selected. See what happens if you change the xpath to: string(sample/formattedText).

Anyhow, this is not what fleXdoc was designed for and I doubt this will ever work. But good luck anyway!

Robert

Nov 26, 2009 at 12:22 PM

Hello Robert

I figured out how to achieve this. I had to make some few modifications on fleXdoc. I'll share it here, if someone is interested in:

XmlQueryProcessor.xslt
Line 254
from: <xsl:value-of select="$dataitem" />
to: <xsl:value-of select="$dataitem" disable-output-escaping="yes" />

XsltExtensions.cs
Line 117
from: return FormatString(context.Current.Value, formatType, ...);
to: return FormatString(context.Current.InnerXml, formatType, ...);

Thanks

Dani

Jun 6, 2011 at 9:02 AM

HI Dani and Robert,

I'm currently spending a lot of time stepping through this area in debug!

 

I've produced a serialized xml document containing the following element which I want simply inserted into the docx - in other words with escaping disabled.

I want this to happen conditionally.

<ReleaseNoteSummary SuppressEscaping="true"> ... *** Word OpenXML markup *** ...</ReleaseNoteSummary> 

The object looks like this:-

 

public class OpenXmlContentNode
    {
        [XmlAttribute("SuppressEscaping")]
        public bool SuppressEscaping = true;

        [XmlText]
        public string Content;
    }

I am now working through your suggested approach Dani, above.

From what I can see, I should be able to use two alternative xslt select statements wrapped in a test of the node attribute "SuppressEscaping".

Am I on the right track?

I'm using another worthwhile CodePlex component - HtmlToOpenXml - I'm so close!

 

Any help or suggestions appreciated.

thx

 

Andy(NZ)

Coordinator
Jun 6, 2011 at 9:11 AM

Your approach could work. If I understand you right, you could perform a check on the attribute using $dataitem, since $dataitem points to the ReleaseNoteSummary-node.

But like I said, this is not a perfect sample of what fleXdoc was designed for. You may want to use the OpenXML SDK directly.

Good luck with it! :)

Jun 6, 2011 at 11:27 PM

Hi Robert, thanks for your reply. I'm sorry to bother you for further technical answers!.

I have achieved the following xml format:-

 

<ReleaseNoteSummary SuppressEscaping="true">
- <![CDATA[ <w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pPr><w:jc w:val="left" /><w:jc w:val="left" /><w:rPr><w:lang w:val="EN" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:....... *** work markup *** ...</w:t></w:r></w:p>
  ]]> 
  </ReleaseNoteSummary>

 

my question is - in the following section of your main xslt:-

 

<xsl:element name="t" namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <xsl:value-of disable-output-escaping="no" select="$dataitem"/>
      </xsl:element>

 

how would I test the existence and value of the attribute "SuppressEscaping" and use an alternative value-of item?

I might try:-

 

<xsl:element name="t" namespace="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <xsl:choose>
          <xsl:when test="$dataitem/@SuppressEscaping = 'true'" >
            <xsl:value-of disable-output-escaping="yes" select="$dataitem"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of disable-output-escaping="no" select="$dataitem"/>
          </xsl:otherwise>
        </xsl:choose>
        
      </xsl:element>

but this isn't working so far. Should I understand that $dataitem is a node reference or node value?

If you have time to help me out a little it would be most appreciated. I am slowly becoming familiar with the internal workings of FlexDoc...

thx

Andy

 

Jun 8, 2011 at 8:21 AM

Decided to pursue a definite course - as you alluded to:-

using the FlexDoc API - the first pass through will put a placeholder in the document, and the content in a symbol table.

I'll then loop through a LINQ list of elements matching the placeholder search - replacing the node with the corresponding content.

A bit more concrete for me with less voodoo - xslt is still a new area - and a vast one.

I would still recommend something like an OpenXMLContentNode Value-of feature in FlexDoc.

thx

Andy

Coordinator
Jul 23, 2013 at 6:36 PM
Come check out the successor of fleXdoc: Docati!
It supports Word 2010 and 2013 as well and runs in the cloud.

I sure hope to welcome you as a Docati user as well! :-)

http://www.docati.com