簡體   English   中英

用xpath和/或xsl-fo格式化斜體/粗體標簽

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

我有類似這樣的問題: 使用XSLT創建帶有嵌套的粗體/斜體標簽的XSL-FO 我想檢測XML文本中的<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>

除了您的XSL輸出HTML和XSL-FO的混合內容外,它實際上似乎還帶有“ bold”和“ italic”標簽。

如果您追求純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中聲明它,並相應地調整模板匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM