3

I would like to insert a space character where the w:tab character is residing in an Open XML document using XSLT.

Here is my style sheet:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" exclude-result-prefixes="w v"> <xsl:output method="text" indent="no" encoding="UTF-8" version="1.0"/> <!-- document root --> <xsl:template match="/"> <!-- root element in document --> <xsl:apply-templates select="w:document"/> </xsl:template> <!-- ****************************start document**************************** --> <xsl:template match="w:document"> <xsl:for-each select="//w:p"> <xsl:apply-templates select="*/w:t"/> <xsl:text>|¤¤</xsl:text> </xsl:for-each> </xsl:template> <!-- get all text nodes within a para --> <xsl:template match="*/w:t"> <xsl:value-of select="."/> </xsl:template> <!-- **************************** end document**************************** --> 

Here is a snippet of my Open XML Document:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14"> <w:body> <w:p w:rsidR="00AC02A3" w:rsidRDefault="00AC02A3"> <w:pPr> <w:pStyle w:val="DefaultText"/> <w:ind w:left="720" w:hanging="720"/> </w:pPr> <w:r> <w:t>1.1</w:t> </w:r> <w:r> <w:tab/> </w:r> <w:r> <w:rPr> <w:u w:val="single"/> </w:rPr> <w:t>C</w:t> </w:r> <w:r> <w:rPr> <w:color w:val="000000"/> <w:u w:val="single"/> </w:rPr> <w:t>ompetitive People</w:t> </w:r> <w:r> <w:rPr> <w:color w:val="000000"/> </w:rPr> <w:t xml:space="preserve"> will always find a way to work out, even when pressed for time. It foll</w:t> </w:r> <w:r> <w:rPr> <w:color w:val="000000"/> </w:rPr> <w:t>d</w:t> </w:r> <w:r> <w:rPr> <w:color w:val="000000"/> </w:rPr> <w:t>ows that anyone can</w:t> </w:r> </w:p> <w:p w:rsidR="00AC02A3" w:rsidRDefault="00AC02A3"> <w:pPr> <w:pStyle w:val="DefaultText"/> </w:pPr> </w:p> <w:p w:rsidR="00AC02A3" w:rsidRDefault="00AC02A3"> <w:pPr> <w:pStyle w:val="DefaultText"/> <w:ind w:left="720" w:hanging="720"/> </w:pPr> <w:r> <w:t>1.2</w:t> </w:r> <w:r> <w:tab/> </w:r> <w:r> <w:rPr> <w:u w:val="single"/> </w:rPr> <w:t>improve their time</w:t> </w:r> <w:r> <w:t xml:space="preserve"> management if th</w:t> </w:r> <w:r> <w:t>e</w:t> </w:r> <w:r> <w:t>y really try ha</w:t> </w:r> <w:r> <w:t>d</w:t> </w:r> <w:r> <w:t xml:space="preserve">rd enough.</w:t> </w:r> </w:p> </w:body> 

Here is the output it produces:

1.1Competitive People will always find a way to work out, even when pressed for time. It follows that anyone can 1.2improve their time management.

I would like to insert a space character between 1.1 and Competitive, and 1.2 and improve.

I assume that I will have to manipulate the following fragments, but I am stuck:

<w:r> <w:t>1.1</w:t> </w:r> <w:r> <w:tab/> </w:r> <w:r> <w:rPr> <w:u w:val="single"/> </w:rPr> <w:t>C</w:t> </w:r> 
1
  • Calling <xsl:for-each select="//w:p"> is a very bad style. It's like using global variables. Please try to work with only <apply-templates/> and <xsl:template match=.../> Commented Nov 19, 2015 at 0:00

1 Answer 1

1

OXML output solution

To output OXML but with w:tab replaced with a space character wrapped in a w:t, use this XSLT:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="w:tab"> <w:t> <xsl:text> </xsl:text> </w:t> </xsl:template> </xsl:stylesheet> 

Text output solution

To output text like this,

1.1 Competitive People will always find a way to work out, even when pressed for time. It folldows that anyone can 1.2 improve their time management if they really try hadrd enough. 

Use this XSLT:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="w:t"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="w:p[.//w:t]"> <xsl:apply-templates/> <xsl:text>&#xa;</xsl:text> </xsl:template> <xsl:template match="w:tab"> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet> 
Sign up to request clarification or add additional context in comments.

1 Comment

Just what I was looking for. Thanks a lot for your help. You made my day.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.