xml - 在 Word Open XML (OOXML) 中用空格字符替换制表符

标签 xml xslt xpath openxml

我想在 w:tab 字符驻留在使用 XSLT 的 Open XML 文档中的位置插入一个空格字符。

这是我的样式表:

<?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 和 Competitive 以及 1.2 和 improve 之间插入一个空格字符。

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

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

最佳答案

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>

关于xml - 在 Word Open XML (OOXML) 中用空格字符替换制表符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792504/

相关文章:

java - XMLUnit 2.0 - 无法避免与自定义元素选择器进行顺序比较

c# - XSLT for-each 仅显示第一行

xslt - 管理 XSL 中的包含内容

sql - SUM 和 COUNT xPath 表达式在 Oracle 11.2 中不起作用

Python 如何从Basic Table获取Scrapy Xpath数据?

java - 解析 XML 时忽略 DTD

javascript - 如何从属性创建文件路径

regex - 如何仅捕获 id 的一部分?

xml - 错误 : S4s-elt-character: Non-whitespace Characters Are Not Allowed In Schema Elements Other Than 'xs:appinfo' And 'xs:documentation'

xslt - 如何在 XSLT 中应用字母数字排序