简体   繁体   中英

format italic/bold tag with xpath and/or xsl-fo

I have an similar issue like this question: Using XSLT to create XSL-FO with nested bold/italic tags . I want to detect <italic> and <bold> tags in XML Text and give it out right formatted with XSLT. I tried it like the solution in the other question but it seems it doesn't work for me. What am I missing?

This is my XML structure:

<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>

This XSL works but without <italic> or <bold> tags:

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

This is how I tried to use the solution on my XML structure:

<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>

Apart from your XSL outputting a mix of HTML and XSL-FO, it does actually seem to pick up "bold" and "italic" tags.

If you are after pure XSL-FO, then looking at the question you have referenced, it doesn't need much work to make it work with your 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>

Of course, one reason why it might not be working, may be if your actual XML has namespace declaration in. In which case, you will need to declare it in your XSLT too and adjust your template matches accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM