在Word Open XML(OOXML)中将制表符替换为空格字符

3

我希望能够使用XSLT在Open XML文档中将w:tab字符所在的位置插入一个空格字符。

这是我的样式表:

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

这是我Open XML文档的片段:
<?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>

这里是它产生的输出:

1.1竞争者总能找到时间锻炼身体,即使时间紧迫。因此,任何人都可以 1.2改善他们的时间管理。

我想在1.1和“竞争者”之间以及1.2和“改善”之间插入一个空格字符。

我认为我将不得不操作以下片段,但我卡住了:

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

调用<xsl:for-each select =“//w:p”>是非常糟糕的风格,就像使用全局变量一样。请尽量只使用<apply-templates/>和<xsl:template match = .../>。 - Matthias M
1个回答

1

OXML输出解决方案

要输出OXML,但将w:tab替换为包含空格字符的w:t,请使用此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>

文本输出解决方案

要输出像这样的文本,

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.

使用此 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>

正是我所需要的。非常感谢你的帮助。你让我的一天变得更美好了。 - tstorli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接