2014-07-09 76 views
0

我有类似的问题:Using XSLT to create XSL-FO with nested bold/italic tags。我想要检测XML文本中的<italic><bold>标记,并将其标记为使用XSLT格式化。 我试过它像其他问题的解决方案,但它似乎不适合我。我错过了什么?使用xpath和/或xsl-fo格式化斜体/粗体标记

这是我的XML结构:

<bibliography> 
    <type1> 
     Some text and <italic>italic Text</italic> and <bold>bold text</bold> 
    </type1> 
    <type2> 
     Some text and <italic>italic Text</italic> and <bold>bold text</bold> 
    </type2> 
</bibliography> 

这XSL工作,但没有<italic><bold>标签:

<xsl:template match="/bibliography/*"> 
    <p> 
     <div class="entry{@type}"> 
    [<xsl:number count="*"/>] 
    <xsl:apply-templates/> 
     </div> 
    </p> 
</xsl:template> 

这是我试图用我的XML结构的解决方案:

<xsl:template match="/bibliography/*"> 
    <p> 
     <div class="entry{@type}"> 
    [<xsl:number count="*"/>] 
    <xsl:apply-templates/> 
     </div> 
    </p> 
</xsl:template> 
<xsl:template match="/"> 
    <div class="entry{@type}"> 
     <p> 
      <fo:root> 
       <fo:page-sequence> 
        <fo:flow> 
         <xsl:apply-templates select="bibliography"/> 
        </fo:flow> 
       </fo:page-sequence> 
      </fo:root> 
     </p> 
    </div> 
</xsl:template> 
<xsl:template match="italic"> 
    <fo:inline font-style="italic"> 
     <xsl:apply-templates select="node()"/> 
    </fo:inline> 
</xsl:template> 

<xsl:template match="bold"> 
    <fo:inline font-weight="bold"> 
     <xsl:apply-templates select="node()"/> 
    </fo:inline> 
</xsl:template> 

回答

1

除了你的XSL输出混合的HTML和d XSL-FO,它实际上似乎拿起了“大胆”和“斜体”标签。

如果你是纯XSL-FO后,再看着你提到的问题,它并不需要太多的工作,使其与您的XML

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

    <xsl:template match="bibliography"> 
     <fo:root> 
      <fo:page-sequence> 
       <fo:flow> 
        <xsl:apply-templates /> 
       </fo:flow> 
      </fo:page-sequence> 
     </fo:root> 
    </xsl:template> 

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

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

    <xsl:template match="italic"> 
     <fo:inline font-style="italic"> 
      <xsl:apply-templates /> 
     </fo:inline> 
    </xsl:template> 
</xsl:stylesheet> 

当然,原因之一是工作可能不起作用,如果您的实际XML具有名称空间声明,则可能是这样。在这种情况下,您还需要在XSLT中声明它,并相应地调整模板匹配。

+0

我读过XSL-Fo仅适用于PDF而不适用于HTML。这是否意味着没有选项可以将fo对象导出为HTML?在没有XSL-FO的情况下,是否可以添加''和''? – Peter

+0

xsl-fo通常用于输出HTML。如果你想在浏览器中显示结果,只需修改它来输出HTML标签。我看到你已经为此提出了另一个问题,我用这个答案的一个变体来回答。我希望它有帮助。 –

相关问题