0

XML および XSLT ファイルを使用して PDF を出力するために FOP を実装しようとしています。

私の問題は次のとおりです。たとえば、行内の単語の位置を修正する必要があります(ただし、テーブルを使用する必要はありません)。

私は次のxmlを持っています:

 <address>
    <Line1 length="32" noLine="5" col="60" />
    <Line2 length="32" noLine="6" col="60">Mr. John Kane</Line2 >
    <Line3 length="32" noLine="7" col="60">15 Street Springfield</Line3 >
    <Line4 length="32" noLine="8" col="60" />
    <Line5 length="32" noLine="9" col="60" />
    <Line6 length="6" noLine="10" col="60">75009</Line6 >
    <Line7 length="25" noLine="10" col="67">Freesberg</Line7 >
    <Line8 length="25" noLine="11" col="67">Idaho</Line8 >
  </address>
  1. は単語length/文の長さです
  2. noLine行番号です
  3. col行内の単語/文の開始位置です

行を作成しましたが、単語/文を行の正しい位置 (列) に挿入できないようです。

これは私の xslt の一部です:

<fo:block font-size="10" font-family="monospace">
<xsl:for-each select="*">
<xsl:variable name="currentNode" select ="name(.)"/>

<xsl:choose>
<xsl:when test="$currentNode = 'address'">
                                                <xsl:for-each select="*">
                                                    <xsl:variable name="length" select ="@length"/>
                                                    <xsl:variable name="noLine" select ="@noLine"/>
                                                    <xsl:variable name="col" select ="@col"/>
                                                    <xsl:variable name="precNoLig" select = "preceding-sibling::*[1]/@noLine"/>
                                                    <xsl:choose>
                                                        <xsl:when test="$precNoLig = $noLine">
                                                            <fo:block font-size="10" font-family="monospace" text-indent="60">
                                                                &#160;<xsl:value-of select="." />
                                                            </fo:block>
                                                        </xsl:when>
                                                        <xsl:otherwise>
                                                            <!--<fo:block font-size="10" font-family="monospace" >-->
                                                                &#x2028;<xsl:value-of select="." />
                                                            <!--</fo:block>-->
                                                        </xsl:otherwise>
                                                    </xsl:choose>
                                                </xsl:for-each>
    </xsl:when>
    </xsl:choose>

    </xsl:for-each>
    </fo:block>

これは次のように期待される出力PDFです。

                                         Mr. John Kane
                                         15 Street Springfield


                                         75009 Freesberg
                                               Idaho

PDF(col)に次の位置があります。

<-----------------60-------------------->
<-----------------60-------------------->Mr. John Kane
<-----------------60-------------------->15 Street Springfield
<-----------------60-------------------->
<-----------------60-------------------->
<-----------------60-------------------->75009 Freesberg
<-----------------67-------------------------->Idaho

どんな助けでも大歓迎です。

4

1 に答える 1

1

少し複雑ですが、これはうまくいくはずです:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:strip-space elements="*" />
  <xsl:output method="text"/>

  <xsl:variable name="sp" select="'                                                                                          '" />

  <xsl:template match="address/*">
    <xsl:variable name="value">
      <xsl:value-of select="." />
      <xsl:if test="following-sibling::*[1]/@noLine = @noLine">
        <xsl:value-of select="$sp" />
      </xsl:if>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="preceding-sibling::*[1]/@noLine = @noLine">
        <xsl:variable name="col" select="preceding-sibling::*[1]/@col + preceding-sibling::*[1]/@length" />
        <xsl:value-of select="concat(substring($sp,1,@col - $col),substring($value,1,@length))" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="preceding-sibling::*"><xsl:text>&#10;</xsl:text></xsl:if>
        <xsl:value-of select="concat(substring($sp,1,@col),substring($value,1,@length))" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

この変換をそのまま機能させるには、前の要素に基づいてパディングする方法に基づいているため、Line..要素がnoLine正しい順序であることが重要です。colそれらが順不同である場合、それはひどく間違っています。

sp変数には、パディングが必要な数以上のスペースが含まれている必要があります。つまり、任意の属性または属性の最大値lengthですcol。このソリューションには、行末にスペースが含まれていません。実際には、指定された長さを埋めるために各行に末尾のスペースを埋め込む方が簡単ですが、おそらくそうしない方が望ましいと思いました。

与えられたサンプルでのみ試しました。他の入力でうまくいかない場合はお知らせください。適応できるかどうかを確認します。

編集:PDF形式が必要であることに気づきました。申し訳ありません。出力を見ただけで、形式に気づきませんでした。うまくいけば、これを必要な形式に適応させることができます (実際には、既に試したものと非常に似ています)。そうでない場合は、出力を表す PDF を手動で作成し、その PDF の XML を質問。

于 2012-09-03T06:41:43.040 に答える