Search

July 2007
M T W T F S S
« Jun   Dec »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Word 2003 xslt file and line breaks in XML data file not working.

July 4th, 2007 by

From: <Paul discussions microsoft com>
Subject: Word 2003 xslt file and line breaks in XML data file not working.
Date: Wed, 6 Jun 2007 05:38:06 -0700
Newsgroups: microsoft.public.office.xml


Hello


I have an XML data file and some of the elements have line breaks in them.


I have a Word 2003 xml document referencing a schema file based on the above XML data file. I save the Word xml file and then convert it to an xslt file with wml2xslt. If I then look at the xml data file using the xslt file the elements with the line breaks are shown without line breaks.


If I view the xml data file with IE then I can see the line breaks in the elements but they are removed by Word.


Can anyone suggest how to get Word to ‘see’ the line breaks and include them in the final output from the xslt file?


Thanks for any help.


Paul


**************************************


From:  d.j.hitchcockathotmaildotcotoduk
Newsgroups: microsoft.public.office.xml
Subject: Re: Word 2003 xslt file and line breaks in XML data file not working.
Date: Mon, 25 Jun 2007 03:49:15 -0700


Hi Paul,


I was having the exact same problem last week, and I (sort of) solved it by editing the xsl file that wml2xslt creates.


Add these namespaces to the .xsl:


xmlns:msxsl=”urn:schemas-microsoft-com:xslt”
xmlns:user=”urn:my-scripts”


Then add the following script and template to your .xsl:


  <msxsl:script language=”C#” implements-prefix=”user”>
    <![CDATA[
      public string beforeNewLine(string original){
        if(original.Contains(“
\\n“)){
          return original.Substring(0, original.IndexOf(“
\\n“));
        }

        return original;
      }

     public string afterNewLine(string original){
        if(original.Contains(“
\\n“)){
          int index = original.IndexOf(“
\\n“);
          if(original.Length>2){
            return original.Substring(index + 2,  original.Length – index – 2);
          }else{
            return “”;
          }
        }

        return original;
      }
    ]]>
  </msxsl:script>

  <xsl:template name=”replaceNewLines”>
    <xsl:param name=”value”></xsl:param>
    <xsl:choose>
      <xsl:when test=’contains($value,”\n”)’>
        <xsl:value-of select=’user:beforeNewLine($value)’/>
        <w:p />
        <xsl:call-template name=”replaceNewLines”>
          <xsl:with-param name =”value” select=”user:afterNewLine($value)”></xsl:with-param>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select=”$value”/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

NOTE: i have used <w:p /> to write the new line – where w is the tag prefix of the “http://schemas.microsoft.com/office/word/2003/wordml” namespace.

Then call the replaceNewLines template on any text that you need to replace.

So instead of using
            <xsl:value-of select=”.” />

use
            <xsl:call-template name=”replaceNewLines”>
              <xsl:with-param name =”value” select=”.” />
            </xsl:call-template>

Now for any node that might contain text with ‘\n’ characters they will be replaced with <w:p /> nodes and will be new lines in the Word output.

This is probably a long winded approach, but it works for me!

**************************************

From: <Paul discussions microsoft com>
Subject: Re: Word 2003 xslt file and line breaks in XML data file not worki
Date: Mon, 25 Jun 2007 05:39:00 -0700
Newsgroups: microsoft.public.office.xml

Thank you so much for the reply. I’ve not yet found a solution and this looks good so I will give it go.

Best regards

Paul

**************************************

From:  d.j.hitchcock
Newsgroups: microsoft.public.office.xml
Subject: Re: Word 2003 xslt file and line breaks in XML data file not worki
Date: Tue, 26 Jun 2007 07:01:31 -0700

Paul,

On second thoughts, you will want to use the actual character code for the new line character ( )- I was just using a text placeholder.

Change the script and the template in the code above to: […]

**************************************

From:  d.j.hitchcock
Newsgroups: microsoft.public.office.xml
Subject: Re: Word 2003 xslt file and line breaks in XML data file not worki
Date: Tue, 26 Jun 2007 07:33:03 -0700

Forget that!

It’s even easier – I didn’t realise that I didn’t have to write my own script to perform those functions.  Just use this template instead:

  <xsl:template name=”replaceNewLines”>
    <xsl:param name=”value />
    <xsl:choose>
     <xsl:when test=”contains($value,’ ’)”>
        <xsl:value-of select=”substring-before($value, ‘ ’)”/>
        <w:p />
        <xsl:call-template name=”replaceNewLines”>
          <xsl:with-param name =”value” select=”substring-after($value, ‘ ’)” />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select=”$value”/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>



No need for script!


end slug


Posted using BlogJet

Posted in QqqJune, XML-XSL Tips | No Comments »



Comments are closed.