2017-05-30 116 views
0

我尝试使用Apache FOP创建PDF文件。许多事情工作得很好,但我不能成功使用嵌套标签。名称“Doe”不以粗体字显示。 非常感谢嵌套标签无法正常工作的xsl fo

这里是我的数据和XSL-FO文件:

数据

<?xml version="1.0" encoding="UTF-8"?> 
<patient> 
    <name>Joe <bold>Doe</bold></name> 
</patient> 

文件

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo"> 
    <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/> 

    <xsl:template match="patient">  
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

     <fo:layout-master-set> 
     <fo:simple-page-master master-name="introA4" page-height="29.7cm" page-width="21cm" margin-top="7cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm"> 
      <fo:region-body/> 
     </fo:simple-page-master> 
     </fo:layout-master-set> 

     <fo:page-sequence master-reference="introA4"> 
     <fo:flow flow-name="xsl-region-body" color="#808285">  

      <fo:block font-size="16pt" space-after="0mm"> 
      <xsl:value-of select="name"/> 
      </fo:block> 

     </fo:flow> 
     </fo:page-sequence> 

    </fo:root> 
    </xsl:template> 

    <xsl:template match="bold"> 
    <fo:inline font-weight="bold" color="red"> 
      <!--xsl:apply-templates select="node()"/--> 
      <!--xsl:apply-templates select="patient/bold"/--> 
      <xsl:apply-templates/> 
      <!--xsl:value-of select="bold"/--> 
    </fo:inline> 
    </xsl:template> 

    <xsl:template match="boldGold"> 
     <fo:inline font-family="OpenSans-ExtraBold" font-weight="bold" color="red"> 
      <xsl:value-of select="boldGold"/> 
     </fo:inline> 
    </xsl:template> 


</xsl:stylesheet> 

回答

2

变化:

<xsl:value-of select="name"/> 

到:

<xsl:apply-templates select="name"/> 

随着xsl:value-of,你刚开name元素的字符串值。通过xsl:apply-templates,您可以指示XSLT处理器为您选择的节点查找并使用最匹配的模板。

另一种方式来工作,这将是使模板name产生fo:block

<xsl:template match="patient">  
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

    <fo:layout-master-set> 
     <fo:simple-page-master master-name="introA4" 
      page-height="29.7cm" page-width="21cm" 
      margin-top="7cm" margin-bottom="2cm" 
      margin-left="2cm" margin-right="2cm"> 
     <fo:region-body/> 
     </fo:simple-page-master> 
    </fo:layout-master-set> 

    <fo:page-sequence master-reference="introA4"> 
     <fo:flow flow-name="xsl-region-body" color="#808285">  
     <xsl:apply-templates /> 
     </fo:flow> 
    </fo:page-sequence> 
    </fo:root> 
</xsl:template> 

<xsl:template match="name"> 
    <fo:block font-size="16pt" space-after="0mm"> 
    <xsl:apply-templates /> 
    </fo:block> 
</xsl:template> 

<xsl:template match="bold"> 
    <fo:inline font-weight="bold" color="red"> 
    <xsl:apply-templates/> 
    </fo:inline> 
</xsl:template> 
+0

谢谢您的回答。它完美地工作,替代解决方案是伟大的。再次感谢! –