问题是由于工作表基础数据结构中 table:table-cell 元素的 number-columns-repeated 属性。更多详情请参考http://user.services.openoffice.org/en/forum/viewtopic.php?f=45&t=29674和http://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 < $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 >= $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 以获得基于模板的解决方案。如果您发现任何错误,请告诉我。