1

for-each ループを含む XSL ファイルがあり、EXSLT プロセッサを使用して追加機能 (文字列パディング) を提供しています。

私ができるようにしたいのは、すべてのフィールドを埋めて、そのフィールドの最長レコードの長さになるようにすることです。たとえば、各名前を最長の名前と同じ長さにし、各レコード番号を最長のレコード番号と同じくらい長くパディングします。

これで大丈夫だと思います。

前もって感謝します。

4

3 に答える 3

4
<xsl:variable name="maxLength">
  <xsl:for-each select="name">
    <xsl:sort select="string-length(.)" data-type="number" />
    <xsl:if test="postion() = last()">
      <xsl:value-of select="string-length(.)" />
    </xsl:if>
  </xsl:for-each>
</xsl:variable>
于 2011-01-04T12:36:16.243 に答える
2

楽しみのために、このスタイルシート:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="vMaxLengthIds">
        <xsl:for-each select="/table/tr[1]/*">
            <xsl:variable name="vPosition" select="position()"/>
            <xsl:for-each select="/table/tr/*[$vPosition]">
                <xsl:sort select="string-length(.)" data-type="number" />
                <xsl:if test="position() = last()">
                    <xsl:value-of select="concat('|',generate-id(),'|')" />
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="vMaxLength"
                  select="/table/tr/*[contains(
                                         $vMaxLengthIds,
                                         concat('|',generate-id(),'|'))]"/>
    <xsl:variable name="vPaddingMask"
                  select="'                                                '"/>
    <xsl:template match="tr">
        <xsl:apply-templates/>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>
    <xsl:template match="td">
        <xsl:apply-templates/>
        <xsl:text> | </xsl:text>
    </xsl:template>
    <xsl:template match="th">
        <xsl:apply-templates/>
        <xsl:text> + </xsl:text>
    </xsl:template>
    <xsl:template match="tr/*/text()">
        <xsl:variable name="vPosition"
                      select="count(../preceding-sibling::*)"/>
        <xsl:value-of
             select="concat(
                        .,
                        substring(
                           $vPaddingMask,
                           1,
                           string-length(
                              $vMaxLength[count(preceding-sibling::*)
                                          = $vPosition])
                           - string-length()))"/>
    </xsl:template>
</xsl:stylesheet>

この入力で:

<table>
    <tr>
        <th>Day</th>
        <th>Month</th>
        <th>Year</th>
    </tr>
    <tr>
        <td>1</td>
        <td>January</td>
        <td>2011</td>
    </tr>
    <tr>
        <td>31</td>
        <td>June</td>
        <td>2011</td>
    </tr>
    <tr>
        <td>11</td>
        <td>February</td>
        <td>2011</td>
    </tr>
</table>

出力:

Day + Month    + Year + 
1   | January  | 2011 | 
31  | June     | 2011 | 
11  | February | 2011 | 
于 2011-01-04T16:16:21.013 に答える
1

C# を使用すると、Saxon 9 や XQSharp などの XSLT 2.0 プロセッサに移行でき、アイテムの最大長を簡単に見つけて、http: //www.xsltfunctions.com/xsl/functx_pad-string-to- のような関数を使用できます。 length.htmlをパディングします。XSLT 1.0 と EXSLT を使用する場合は、http://www.exslt.org/math/functions/max/index.htmlを使用して最大値を見つけ、http://www.dpawson.co のソリューションを使用します。 .uk/xsl/sect2/padding.htmlをパディングします。

于 2011-01-04T12:35:23.603 に答える