简体   繁体   English

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

[英]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 . 我有类似这样的问题: 使用XSLT创建带有嵌套的粗体/斜体标签的XSL-FO I want to detect <italic> and <bold> tags in XML Text and give it out right formatted with XSLT. 我想检测XML文本中的<italic><bold>标记,并将其正确地显示为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: 这是我的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>

This XSL works but without <italic> or <bold> tags: 此XSL有效,但没有<italic><bold>标记:

<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: 这就是我尝试在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>

Apart from your XSL outputting a mix of HTML and XSL-FO, it does actually seem to pick up "bold" and "italic" tags. 除了您的XSL输出HTML和XSL-FO的混合内容外,它实际上似乎还带有“ bold”和“ italic”标签。

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

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. 当然,它可能不起作用的原因之一可能是您的实际XML中是否包含名称空间声明。在这种情况下,您也需要在XSLT中声明它,并相应地调整模板匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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