我正在编写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> </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中已经存在的换行符。
答案 0 :(得分:0)
我现在无法对此进行测试,但是可以将其作为您的导出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> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</row>
</xsl:for-each>
</top>
</xsl:template>
</xsl:stylesheet>