【发布时间】:2019-06-05 20:11:14
【问题描述】:
我正在编写 XSLT 过滤器以将 XML 数据导入 Libreoffice-Calc,然后在 Libreoffice 中进行修改后将其导出回 XML。
在源 XML 中,某些字段包含多行。当我使用我的自定义脚本将它们导入 Libreoffice-Calc 时,它们会被保留并正确显示在电子表格中。但是,当我将数据导出回 xml 文件时,会删除换行符。
这些换行符是我需要保留的数据的重要组成部分。如何让它们显示在输出中?
我已经尝试过的一些事情:
- 使用“copy-of”而不是“value-of”
- 为 value-of 设置 disable-output-escaping 属性
- 将 fn:data() 函数应用于 copy-of 的 select 语句,(出现“未找到函数数据”错误)
这是一个例子:
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> </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> </xsl:text>
</xsl:template>
</xsl:stylesheet>
这是 michael.hor257k 的答案的修改版本,它正确解决了导出 LibreOffice 中插入的换行符的问题。第二部分是换行模板匹配,负责处理导入的 xml 中已经存在的换行。
【问题讨论】:
-
导入文件有关系吗?如果在 LibreOffice 本身中插入换行符,会得到不同的结果吗?
-
在LibreOffice中插入换行符的结果是一样的。该脚本的目的是提供一种使用 LibreOffice 编辑现有 xml 文件的简单方法,因此必须导入数据。
-
好的,但是如果在 LibreOffice 中插入换行符时结果相同,那么显然问题出在导出端,而导入无关紧要。
-
是的,我猜导入与问题无关。不过,它仍然有助于重现问题。
-
实际上,导入和插入行之间有一个重要的区别。当导入中有换行符时,它们显示为
,而当在 LibreOffice 中插入行时,它们显示为单独的 块。
标签: xml xslt libreoffice-calc odt