使用xpath和/或xsl-fo格式化斜体/粗体标记

时间:2014-07-09 18:34:03

标签: xml xslt xpath xsl-fo

我有类似这个问题的类似问题:Using XSLT to create XSL-FO with nested bold/italic tags。我想检测XML Text中的<italic><bold>标记,并使用XSLT格式化它。 我试着像其他问题的解决方案,但似乎它对我不起作用。我错过了什么?

这是我的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 个答案:

答案 0 :(得分:1)

除了你的XSL输出混合的HTML和XSL-FO之外,它实际上似乎已经选择了&#34;粗体&#34;和&#34;斜体&#34;标签

如果您使用的是纯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中声明它并相应地调整模板匹配。

相关问题