2012-04-24 3 views
1

나는 다음과 같은 XML이 :값없이 필드에 XSL-FO 생성 PDF의 간격/패딩을 삽입하려면 어떻게합니까?

Value 1: This is one Value 2: Number two 
Value 3:     Value 4: Last value 

주의 사이의 간격/패딩 : 아파치 FOP/XSL-FO를 사용

<sample> 
    <value1>This is one</value1> 
    <value2>Number two</value2> 
    <value4>Last value</value4> 
</sample> 

나는이 유사 찾고 PDF 싶습니다 "값 3" 및 "값 4 :".

다음 변형은 내가 원하는 결과를 제공합니다. 하지만 지나치게 복잡해 보이며 (많은 가치를 지닌 실제 PDF에서는 성능이 좋지 않을 수 있음)

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

    <xsl:template match="sample"> 
     <fo:root> 
      <fo:layout-master-set> 
       <fo:simple-page-master master-name="page-layout"> 
        <fo:region-body margin="10mm" region-name="body"/> 
       </fo:simple-page-master> 
      </fo:layout-master-set> 
      <fo:page-sequence master-reference="page-layout"> 
       <fo:flow flow-name="body"> 
        <fo:block> 
         <xsl:variable name="pad">        
          <xsl:choose> 
           <xsl:when test="value1">5</xsl:when> 
           <xsl:otherwise>25</xsl:otherwise> 
          </xsl:choose> 
         </xsl:variable> 
         <fo:inline padding-right="{$pad}mm">Value 1: <xsl:value-of select="value1"/></fo:inline> 
         Value 2: <xsl:value-of select="value2"/> 
        </fo:block> 
        <fo:block> 
         <xsl:variable name="pad"> 
          <xsl:choose> 
           <xsl:when test="value3">5</xsl:when> 
           <xsl:otherwise>25</xsl:otherwise> 
          </xsl:choose> 
         </xsl:variable> 
         <fo:inline padding-right="{$pad}mm">Value 3: <xsl:value-of select="value3"/></fo:inline> 
         Value 4: <xsl:value-of select="value4"/> 
        </fo:block> 
       </fo:flow> 
      </fo:page-sequence> 
     </fo:root> 
    </xsl:template> 

</xsl:stylesheet> 

구현 방법은 더 간단합니까?

는 업데이트 :

<xsl:template name="field"> 
    <xsl:param name="txt"/> 
    <xsl:param name="val"/> 
    <xsl:param name="pad"/> 
    <xsl:variable name="p"> 
     <xsl:choose> 
      <xsl:when test="$val">5</xsl:when> 
      <xsl:otherwise> 
       <xsl:choose> 
        <xsl:when test="$pad"><xsl:value-of select="$pad"/></xsl:when> 
        <xsl:otherwise>25</xsl:otherwise> 
       </xsl:choose> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:variable> 
    <fo:inline padding-right="{$p}mm"><xsl:value-of select="$txt"/>:</fo:inline> 
    <fo:inline keep-with-previous="always" padding-right="5mm" font-weight="bold"><xsl:value-of select="$val"/></fo:inline> 
</xsl:template> 

과 같이 사용할 수 있습니다 : 템플릿은 선택 세 번째 소요

<xsl:call-template name="field"> 
    <xsl:with-param name="txt">Value 1</xsl:with-param> 
    <xsl:with-param name="val"><xsl:value-of select="sample/value1"/></xsl:with-param> 
</xsl:call-template> 

나는 템플릿 "필드"로 위의 리팩토링 매개 변수, 패드. 지정된 경우 값이 채우기로 사용됩니다.

David의 템플릿 (수락 된 답변 참조)은 필요한 경우 채우기 오른쪽 특성을 덮어 쓰는 간단한 if-contruct를 사용합니다.

답변

2

아마 이것은 브라우저에서 실행되지 않기 때문에 xslt2를 사용하지 않는 이유가 있습니다. (xslt2를 사용하지 않는 이유는 어쨌든 무료 saxon 9 구현이 여기에서 잘 작동 할 것이므로 Java를 사용하기 때문입니다).

하지만 여기에 XSLT 1 버전이 리팩토링되었습니다.

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

    <xsl:template match="sample"> 
     <fo:root> 
      <fo:layout-master-set> 
       <fo:simple-page-master master-name="page-layout"> 
        <fo:region-body margin="10mm" region-name="body"/> 
       </fo:simple-page-master> 
      </fo:layout-master-set> 
      <fo:page-sequence master-reference="page-layout"> 
       <fo:flow flow-name="body"> 
     <xsl:call-template name="twoval"> 
      <xsl:with-param name="a" select="value1"/> 
      <xsl:with-param name="ahead" select="'Value 1'"/> 
      <xsl:with-param name="b" select="value2"/> 
      <xsl:with-param name="bhead" select="'Value 2'"/> 
     </xsl:call-template> 
     <xsl:call-template name="twoval"> 
      <xsl:with-param name="a" select="value3"/> 
      <xsl:with-param name="ahead" select="'Value 3'"/> 
      <xsl:with-param name="b" select="value4"/> 
      <xsl:with-param name="bhead" select="'Value 4'"/> 
     </xsl:call-template> 
       </fo:flow> 
      </fo:page-sequence> 
     </fo:root> 
    </xsl:template> 


<xsl:template name="twoval"> 
<xsl:param name="a"/> 
<xsl:param name="ahead"/> 
<xsl:param name="b"/> 
<xsl:param name="bhead"/> 
<fo:block> 
<fo:inline padding-right="25mm"> 
    <xsl:if test="$a"><xsl:attribute name="padding-right">5mm</xsl:attribute></xsl:if> 
    <xsl:value-of select="$ahead"/><xsl:text>: </xsl:text> 
    <xsl:value-of select="$a"/> 
</fo:inline> 
<xsl:text> </xsl:text> 
<xsl:value-of select="$bhead"/><xsl:text>: </xsl:text> 
<xsl:value-of select="$b"/> 
</fo:block> 
</xsl:template> 

</xsl:stylesheet> 
+0

이것은 결국 내가 쓴 것과 비슷하지만 xls : 패딩 변수를 설정하도록 선택했습니다. 필요한 때에 속성을 재정의하는 것이 더 우아합니다. – slu