【问题标题】:OpenOffice bug while exporting from Calc using xslt filters使用 xslt 过滤器从 Calc 导出时出现 OpenOffice 错误
【发布时间】:2011-04-30 11:22:06
【问题描述】:

我在 Calc 中有一个基于行的数据,我想使用 xslt 过滤器将其导出到 xml。过滤器正常工作,除非两个相邻列中的值相同。比如看下面的数据……

SrNo Col2 Col3 Col4 Col5 1 PQR 123 567 LMN 2 OPQ 665 786 BCD 3 欧元 443 443 UFF 4 OLE 345 887 JAS 5 EJR 565 565 OEP

对于上面的数据,这个错误只发生在第 3 行和第 5 行。由于某种原因,过滤器跳过 col4 并从 col5 中取值。对于其余数据,导出工作得非常好。这是 xslt 代码...

<row>
<col1><xsl:value-of select="table:table-cell[1]"/></col1>
<col2><xsl:value-of select="table:table-cell[2]"/></col2>
<col3><xsl:value-of select="table:table-cell[3]"/></col3>
<col4><xsl:value-of select="table:table-cell[4]"/></col4>
<col5><xsl:value-of select="table:table-cell[5]"/></col5>
</row>

有人可以对此提供任何意见吗?这很奇怪,因此我很难受。顺便说一句,我正在使用 OpenOffice 3.1.1 (Build 9420) 和 xslt 2.0。

【问题讨论】:

    标签: xslt export openoffice-calc


    【解决方案1】:

    Rohit 的代码很棒;它绝对帮助我朝着正确的方向前进。我很难让 XSLT 2.0 代码在我的 LibreOffice 安装中工作,所以我将代码转换为使用 XSLT 1.0(命名模板而不是函数调用,以及能够将节点传递给递归函数的扩展)。如果有人需要,我的代码如下。请注意,它不是通用代码 - 您需要将我的字段替换为您自己的。

    此特定示例将电子表格导出到 XCode 可识别的有效 .plist 文件中。它已经在 Vista 上运行 LibreOffice 3.5 进行了测试。

    <?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:xt="http://www.jclark.com/xt"
        extension-element-prefixes="xt"
        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:output method = "xml" indent = "yes" encoding = "UTF-8" omit-xml-declaration = "no" doctype-system = "http://www.apple.com/DTDs/PropertyList-1.0.dtd" doctype-public = "-//Apple//DTD PLIST 1.0//EN" />
    
        <xsl:template name="getColumnValue">
            <xsl:param name="tableRow"/>
            <xsl:param name="colIndex"/>
            <xsl:param name="currentIndex"/>
            <xsl:choose>
                <xsl:when test="$currentIndex &lt; $colIndex">
                    <xsl:variable name="repeatColumns" select="xt:node-set($tableRow)/table:table-cell[$currentIndex]/@table:number-columns-repeated"/>
                    <xsl:choose>
                        <xsl:when test="$repeatColumns">
                            <xsl:choose>
                                <xsl:when test="$currentIndex + $repeatColumns - 1 &gt;= $colIndex">
                                    <xsl:value-of select="xt:node-set($tableRow)/table:table-cell[$currentIndex]"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:variable name = "recursiveResult">
                                        <xsl:call-template name="getColumnValue">
                                            <xsl:with-param name="tableRow" select="$tableRow"/>
                                            <xsl:with-param name="colIndex" select="$colIndex - $repeatColumns + 1"/>
                                            <xsl:with-param name="currentIndex" select="$currentIndex + 1"/>
                                        </xsl:call-template>
                                    </xsl:variable>
                                    <xsl:value-of select="$recursiveResult"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:variable name = "recursiveResult">
                                <xsl:call-template name="getColumnValue">
                                    <xsl:with-param name="tableRow" select="$tableRow"/>
                                    <xsl:with-param name="colIndex" select="$colIndex"/>
                                    <xsl:with-param name="currentIndex" select="$currentIndex + 1"/>
                                </xsl:call-template>
                            </xsl:variable>
                            <xsl:value-of select="$recursiveResult"/>
    
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="xt:node-set($tableRow)/table:table-cell[$colIndex]"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <!-- By setting the PropertyValue "URL" in the properties used in storeToURL(), -->
        <!-- we can pass a single parameter to this stylesheet.                         -->
        <!-- Caveat: If we use the "URL" property in the stylesheet and call in OOo     -->
        <!-- from the menu "File" > "Export...", OOo assigns a target URL. And that     -->
        <!-- might not be what we want.                                                 -->
        <xsl:param name="targetURL"/>
    
        <xsl:variable name="exportDate">
            <xsl:choose>
                <xsl:when test="string-length(substring-before($targetURL,';'))=10">
                    <xsl:value-of select="substring-before($targetURL,';')"/>
                </xsl:when>
                <xsl:when test="string-length($targetURL)=10">
                    <xsl:value-of select="$targetURL"/>
                </xsl:when>
            </xsl:choose>
        </xsl:variable>
    
        <xsl:variable name="exportUser">
            <xsl:if test="string-length(substring-after($targetURL,';'))>0">
                <xsl:value-of select="substring-after($targetURL,';')"/>
            </xsl:if>
        </xsl:variable>
    
        <xsl:template match="/">
            <plist version="1.0">
                <dict>
                    <key>Animations</key>
                    <array>
                        <!-- Process all tables -->
                        <xsl:apply-templates select="//table:table"/>
                    </array>
                </dict>
            </plist>
        </xsl:template>
    
    
        <xsl:template match="table:table">
            <!-- Process all table-rows after the column labels in table-row 1 -->
            <xsl:for-each select="table:table-row">
                <xsl:if test="position()>1">
                    <dict>
                        <key>character</key>
                        <string>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="1"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </string>
    
                        <key>animation</key>
                        <string>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="2"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </string>
    
                        <key>cycle</key>
                        <xsl:variable name="cycleTmp">
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="3"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </xsl:variable>
                        <xsl:if test="$cycleTmp > 0">
                            <true/>
                        </xsl:if>
                        <xsl:if test="$cycleTmp = 0">
                            <false/>
                        </xsl:if>
    
                        <key>frames</key>
                        <integer>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="4"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </integer>
    
                        <key>randomSpeedPercent</key>
                        <integer>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="5"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </integer>
    
                        <key>spriteNameRoot</key>
                        <string>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="6"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </string>
    
                        <key>spriteSheetName</key>
                        <string>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="7"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </string>
    
                        <key>anchorX</key>
                        <integer>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="11"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </integer>
    
                        <key>anchorY</key>
                        <integer>
                            <xsl:call-template name="getColumnValue"> <xsl:with-param name="colIndex" select="12"/> <xsl:with-param name="currentIndex" select="1"/> <xsl:with-param name="tableRow" select="."/> </xsl:call-template>
                        </integer>
    
                    </dict>
                </xsl:if>
            </xsl:for-each>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 感谢您发布代码。如果您可以对我的回答进行投票(如果有用的话)也会有所帮助:)
    • 哇。我花了整整一夜才找到这个问题并寻找解决方案。
    【解决方案2】:

    问题是由于工作表基础数据结构中 table:table-cell 元素的 number-columns-repeated 属性。更多详情请参考http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=29674http://user.services.openoffice.org/en/forum/viewtopic.php?f=9&t=11865

    虽然后一个链接声称已经解决了问题,但解决方案并不是我想要的。我需要一个简单的基于索引的解决方案,它允许更灵活的 xml 生成。这是我试图解决的问题。

    我使用 xslt 2.0 来使用用户定义的函数。这是样式表...

    <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="no"/>
    
    <xsl:function name="my:getColumnValue">
        <xsl:param name="tableRow" as="node()"/>
        <xsl:param name="colIndex"/>
        <xsl:param name="currentIndex"/>
        <xsl:choose>
            <xsl:when test="$currentIndex &lt; $colIndex">
                <xsl:variable name="repeatColumns" select="$tableRow/table:table-cell[$currentIndex]/@table:number-columns-repeated"/>
                <xsl:choose>
                    <xsl:when test="$repeatColumns">
                        <xsl:choose>
                            <xsl:when test="$currentIndex + $repeatColumns - 1 &gt;= $colIndex"><xsl:value-of select="$tableRow/table:table-cell[$currentIndex]"/></xsl:when>
                            <xsl:otherwise><xsl:value-of select="my:getColumnValue($tableRow, $colIndex - $repeatColumns + 1, $currentIndex + 1)"/></xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise><xsl:value-of select="my:getColumnValue($tableRow, $colIndex, $currentIndex + 1)"/></xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise><xsl:value-of select="$tableRow/table:table-cell[$colIndex]"/></xsl:otherwise>
        </xsl:choose>
    </xsl:function>
    
    <xsl:template match="//table:table">
        <Tests>
            <!-- Process all table rows -->
            <xsl:variable name="colCount" select="count(table:table-row[1]/table:table-cell)"/>
            <xsl:for-each select="table:table-row">
                <xsl:if test="position() > 1">
                <Test>
                    <SrNo><xsl:value-of select="my:getColumnValue(.,1,1)"/></SrNo>
                    <Name><xsl:value-of select="my:getColumnValue(.,2,1)"/></Name>
                    <Age><xsl:value-of select="my:getColumnValue(.,3,1)"/></Age>
                    <Height><xsl:value-of select="my:getColumnValue(.,4,1)"/></Height>
                    <Address><xsl:value-of select="my:getColumnValue(.,5,1)"/></Address>
                </Test>
                </xsl:if>
            </xsl:for-each>
        </Tests>
    </xsl:template>
    

    上面使用的标签只是占位符。请在您的 xslt.xml 中用适当的替换它们。此解决方案受到 xslt 处理器允许的递归调用数量的限制。

    如果 xslt 1.0 支持发送节点作为参数,那么我们可以尝试替换上面的 udf 以获得基于模板的解决方案。如果您发现任何错误,请告诉我。

    【讨论】:

      【解决方案3】:

      Marcel 的解决方案看起来很复杂,但效果很好,而且适应起来并不复杂。

      所以,再次简而言之: 您基本上将整个模板复制并粘贴到您的 xsl 中:

      <xsl:template name="getColumnValue"> ... </>
      

      然后,将所有标准 table:row (失败)替换为

      <xsl:call-template name="getColumnValue"> 
          <xsl:with-param name="colIndex" select="1"/> 
          <xsl:with-param name="currentIndex" select="1"/> 
          <xsl:with-param name="tableRow" select="."/> 
      </xsl:call-template>
      

      请务必调整“colIndex”值,但将 currentIndex 保留为 1! 最后,将标题的 xt 行添加到您的。

      xmlns:xt="http://www.jclark.com/xt"
      extension-element-prefixes="xt"
      

      完美,马塞尔!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-09
        • 1970-01-01
        • 2020-06-04
        • 1970-01-01
        • 2014-11-11
        • 1970-01-01
        • 1970-01-01
        • 2012-11-19
        相关资源
        最近更新 更多