从 libreoffice calc 导出到 xml 时如何保留换行符?

How do I preserve line breaks when exporting from libreoffice calc to xml?

我正在编写 XSLT 过滤器以将 XML 数据导入 Libreoffice-Calc,然后在 Libreoffice 中进行修改后将其导出回 XML。

在来源 XML 中,一些字段包含多行。当我使用我的自定义脚本将它们导入 Libreoffice-Calc 时,它们会被保留并正确显示在电子表格中。但是,当我将数据导出回 xml 文件时,换行符被删除。

这些换行符是我需要保留的数据的重要部分。我怎样才能让它们出现在输出中?

一些我已经尝试过的东西:

这是一个例子:

import.xslt

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0">

      <office:body>
        <office:spreadsheet>
          <table:table>

            <!-- Rows -->
            <xsl:for-each select="top/row">
              <table:table-row>

                <table:table-cell>
                  <text:p><xsl:value-of select="column_1"/></text:p>
                </table:table-cell>

                <table:table-cell>
                  <text:p><xsl:value-of select="column_2"/></text:p>
                </table:table-cell>

              </table:table-row>
            </xsl:for-each>

          </table:table>
        </office:spreadsheet>
      </office:body>
    </office:document-content>
  </xsl:template>
</xsl:stylesheet>

export.xslt

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
  xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
  xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
  exclude-result-prefixes="office table text">

  <xsl:output method = "xml" indent = "yes" encoding = "UTF-8" omit-xml-declaration = "no"/>

  <xsl:template match="office:spreadsheet">
    <xsl:for-each select="table:table/table:table-row">
      <row>
        <xsl:for-each select="table:table-cell">
          <xsl:choose>

            <xsl:when test="position()=1">
              <column_1><xsl:value-of select="."/></column_1>
            </xsl:when>

            <xsl:when test="position()=2">
              <column_2><xsl:value-of select="."/></column_2>
            </xsl:when>

          </xsl:choose>
        </xsl:for-each>
      </row>
    </xsl:for-each>
  </xsl:template>

<xsl:template match="/">
  <top>
    <xsl:apply-templates select="//office:spreadsheet"/>
  </top>
</xsl:template>

</xsl:stylesheet>

xml_file.xml

<?xml version="1.0" encoding="UTF-8"?>
<top>
  <row>
    <column_1>datum 1</column_1>
    <column_2>datum 2</column_2>
  </row>
  <row>
    <column_1>datum 3
    datum 4 (should appear on line below datum 3)</column_1>
    <column_2> datum 5 </column_2>
  </row>
</top>

在 Libreoffice-Calc 中打开 xml_file.xml 后(使用导入文件),进行一些微不足道的更改,然后将其导出回 xml_file.xml:

expected_output.xml

<?xml version="1.0" encoding="UTF-8"?>
<top>
  <row>
    <column_1>datum 1</column_1>
    <column_2>datum 2</column_2>
  </row>
  <row>
    <column_1>datum 3
    datum 4 (should appear on line below datum 3)</column_1>
    <column_2> datum 5 </column_2>
  </row>
</top>

actual_output.xml

<?xml version="1.0" encoding="UTF-8"?>
<top>
  <row>
    <column_1>datum 1</column_1>
    <column_2>datum 2</column_2>
  </row>
  <row>
    <column_1>datum 3 datum 4 (should appear on line below datum 3)</column_1>
    <column_2> datum 5 </column_2>
  </row>
</top>

编辑:

好的,我有一些有用的东西:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
  xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
  xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
  exclude-result-prefixes="office table text">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/office:document">
    <top>
      <xsl:for-each select="office:body/office:spreadsheet/table:table/table:table-row">
        <row>
          <xsl:for-each select="table:table-cell">
            <xsl:element name="column_{position()}">
              <xsl:for-each select="text:p">
                <xsl:apply-templates/>
                <xsl:if test="position() != last()">
                  <xsl:text>&#10;</xsl:text>
                </xsl:if>
              </xsl:for-each>
            </xsl:element>
          </xsl:for-each>
        </row>
      </xsl:for-each>
    </top>
  </xsl:template>

  <xsl:template match="text:line-break">
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

这是 michael.hor257k 答案的修改版本,它正确解决了导出在 LibreOffice 中插入的换行符的问题。第二部分是换行符模板匹配,它负责处理导入的 xml.

中已经存在的换行符

我现在无法对此进行测试,但请尝试将其作为您的导出 XSLT:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
exclude-result-prefixes="office table text">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/office:document">
    <top>
        <xsl:for-each select="office:body/office:spreadsheet/table:table/table:table-row">
            <row>
                <xsl:for-each select="table:table-cell">
                    <xsl:element name="column_{position()}">
                        <xsl:for-each select="text:p">
                            <xsl:value-of select="."/>
                            <xsl:if test="position() != last()">
                                <xsl:text>&#10;</xsl:text>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
            </row>
        </xsl:for-each>
    </top>
</xsl:template>

</xsl:stylesheet>