1

Calcに行ベースのデータがあり、xsltフィルターを使用してxmlにエクスポートしたいと思います。隣接する2つの列の値が同じである場合を除いて、フィルターは正しく機能します。たとえば、以下のデータを参照してください...

SrNo Col2 Col3 Col4 Col5
1 PQR 123 567 LMN
2 OPQ 665 786 BCD
3 EUR 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>

誰かがこれについて何か入力をしてくれますか?それはかなり奇妙で、私はこれのためにひどく立ち往生しています。ところで、私はxslt2.0でOpenOffice3.1.1(ビルド9420)を使用しています。

4

3 に答える 3

2

Rohit さんの素晴らしいコードでした。それは間違いなく私を正しい方向に導くのに役立ちました. LibreOffice インストールで XSLT 2.0 コードを動作させるのに苦労していたので、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>
于 2012-07-24T03:49:44.353 に答える
0

この問題は、シートの基になるデータ構造の 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 &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 内の適切なものに置き換えてください。このソリューションは、xslt プロセッサによって許可される再帰呼び出しの数によって制限されます。

xslt 1.0 が送信ノードをパラメーターとしてサポートしている場合、上記の udf を置き換えて、テンプレート ベースのソリューションを取得できます。バグを見つけたら、私に知らせてください。

于 2011-05-04T07:17:20.093 に答える