问题
I am trying to transform an .ods
file (content.xml
file from the zip) with XSLT in order to produce a desired .xml
file.
XSLT uses "fixed" positions of the elements to get the content, but in my .ods
file I have many blank fields and I don't know how to make them count in XSLT.
Furthermore, I did some experiments with content.xml
to find out if these blank(empty) cells are saved or not.
In content.xml
I found something like:
<table:table-column table:style-name="co1" table:number-columns-repeated="16384" table:default-cell-style-name="ce1"/>
<table:table-row table:number-rows-repeated="1048576" table:style-name="ro1">
Are these values somehow (for example with mathematical calculation) representing the empty cells or actual location of the values in the .ods
file?
I share my documents here for you to have more clear ideas
The .ods
example:
This is my XSLT file:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:espd="urn:com:grow:espd:02.00.00" xmlns:cac="urn:X-test:UBL:Pre-
award:CommonAggregate" xmlns:cbc="urn:X-test:UBL:Pre-award:CommonBasic"
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="office:spreadsheet/table:table">
<xsl:variable name="test" select="table:table-row/table:table-cell"/>
<p><xsl:value-of select="$test/text:p[1]"/></p>
</xsl:template>
And the output is:
burak burak5 burak6 burak2 burak3 burak4 burak7 burak9 burak8 burak10
Question:
How to get the single values from the cells by applying a transformation on content.xml
?
(For example: how to reach just cell D4
?)
回答1:
How to get the single values from the cells by applying a transformation on
content.xml
?
The XML data in the .ods
file's content.xml
is encoded in this way (always prefixed with the table:
namespace):
- Each
table-cell
is contained intable-row
s - Empty
table-cell
s are encoded empty with RLE (Run-Length-Encoding) indicated bynumber-columns-repeated="..."
attributes and must be skipped but counted - Empty
table-row
s are encoded with RLE, too, indicated by thenumber-rows-repeated="..."
attribute table-columns
seem to be used only at the beginning
So, to get a specific cell, e.g. D4
=4:4
, the table-row
s including the skipped ones have to be counted:
D4 = 4:4 = Get the fourth `table-row`, add one cell D1, then add number-columns-repeated="2"
This is some XSLT-1.0 code (also usable with 2.0 and 3.0) with GetCellValue
examples:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:espd="urn:com:grow:espd:02.00.00"
xmlns:cac="urn:X-test:UBL:Pre-award:CommonAggregate"
xmlns:cbc="urn:X-test:UBL:Pre-award:CommonBasic" exclude-result-prefixes="xs fn office style table text espd cac cbc">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="str" select="'x:1 y:4'" /> <!-- define some coord system -->
<xsl:template match="/office:document-content/office:body/office:spreadsheet/table:table">
Table dimensions: <xsl:call-template name="GetDimensions" />
Value at 5x8: <xsl:call-template name="GetCellValue">
<xsl:with-param name="x" select="5" />
<xsl:with-param name="y" select="8" />
</xsl:call-template>
Value at 1x4: <xsl:call-template name="GetCellValue"> <!-- use string defined above -->
<xsl:with-param name="x" select="substring-after(substring-before($str,' '),'x:')" />
<xsl:with-param name="y" select="substring-after($str,'y:')" />
</xsl:call-template>
</xsl:template>
<xsl:template name="GetCellValue">
<xsl:param name="x" />
<xsl:param name="y" />
<xsl:variable name="targetRow" select="table:table-row[sum(preceding-sibling::*/@table:number-rows-repeated) + position() - count(preceding-sibling::*/@table:number-rows-repeated)= $y]" />
<xsl:variable name="targetCell" select="$targetRow/table:table-cell[sum(preceding-sibling::*/@table:number-columns-repeated) + position() - count(preceding-sibling::*/@table:number-columns-repeated) <= $x]" />
<xsl:copy-of select="$targetCell[last()]/text:p/text()" />
</xsl:template>
<xsl:template name="GetDimensions">
<xsl:variable name="firstRow" select="table:table-row[1]/table:table-cell" />
<xsl:variable name="firstColumn" select="table:table-row" />
<xsl:variable name="width" select="count($firstRow)+ sum($firstRow/@table:number-columns-repeated) - count($firstRow/@table:number-columns-repeated)" />
<xsl:variable name="height" select="count($firstColumn)+ sum($firstColumn/@table:number-rows-repeated) - count($firstColumn/@table:number-rows-repeated)" />
<xsl:value-of select="concat($width,'x',$height)" />
</xsl:template>
</xsl:stylesheet>
Output is:
Table dimensions: 5x12
Value at 5x8: burak9
Value at 1x4: burak4
EDIT:
I modified one xsl:call-template
to be used with a string input of a custom format, e.g. x:1 y:4
.
Edit2:
An XSLT-2.0 version which can retrieve multiple cells at once as XML elements can be found in this answer.
来源:https://stackoverflow.com/questions/45327810/how-to-get-any-type-of-content-of-specific-ods-cells-in-xslt