xml 使用 XSLT 创建带有嵌套粗体/斜体标签的 XSL-FO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7038895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:53:23  来源:igfitidea点击:

Using XSLT to create XSL-FO with nested bold/italic tags

xmlxsltxsl-fo

提问by dce

I'm creating xslt file, which should convert my xml into xsl-fo.

我正在创建 xslt 文件,它应该将我的 xml 转换为 xsl-fo。

XML is something like that:

XML 是这样的:

<doc>
  <par>
    <point>
      <text>some text</text>
    </point>
  </par>
</doc>

Of course, there are many paragraphs and points in document. I would like to add possibility to format "some text", for example

当然,文档中有很多段落和要点。例如,我想添加格式化“某些文本”的可能性

<bold>bolded text</bold> and <italic>italic</italic>

Should give

应该给

<fo:block><fo:inline font-weight="bold">bolded text</fo:inline> and <fo:inline font-style="italic">italic</fo:inline>

Now I have quite simple xslt document, like this:

现在我有非常简单的 xslt 文档,如下所示:

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
  <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
  <xsl:param name="versionParam" select="'1.0'"/> 
  <xsl:template match="doc">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <fo:layout-master-set>
        <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
          <fo:region-body margin="2cm"/>
          <fo:region-before margin="0.2cm" extent="1.5cm"/>
        </fo:simple-page-master>
      </fo:layout-master-set>
      <fo:page-sequence master-reference="simpleA4">
        <fo:static-content flow-name="xsl-region-before"  margin-right="1cm"> 
            <fo:block text-align="start" margin-top="0.2cm">
            <fo:block text-align="end" margin-top="0.2cm">
                Page <fo:page-number/> of <fo:page-number-citation ref-id="terminator"/>
            </fo:block>
        </fo:static-content>
        <fo:flow flow-name="xsl-region-body">
          <xsl:apply-templates select="par/point"/>
          <fo:block id="terminator"/>
        </fo:flow>
      </fo:page-sequence>
    </fo:root>
  </xsl:template>
  <xsl:template match="point">
    <fo:block font-size="16pt" space-after="5mm">
        <xsl:value-of select="tresc"/>
    </fo:block>
  </xsl:template>
</xsl:stylesheet>

Should I add another template (bold, italic)? How am I supposed to call it in "text" node?

我应该添加另一个模板(粗体、斜体)吗?我应该如何在“文本”节点中调用它?

I found some solution:

我找到了一些解决方案:

<xsl:template match="//bold">
    <fo:inline font-weight="bold">
        <xsl:value-of select="current()"></xsl:value-of>
    </fo:inline>
</xsl:template>

But it didn't work for me. Output xsl-fo dosn't contain any fo:inline.

但它对我不起作用。输出 xsl-fo 不包含任何 fo:inline。

回答by Emiliano Poggi

Look at this example. It clearly shows how to hanlde inline nodes.

看看这个例子。它清楚地显示了如何处理内联节点。

[XSLT 1.0]

[XSLT 1.0]

<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 indent="yes"/>

    <xsl:template match="doc">
        <fo:root>
            <fo:page-sequence>
                <fo:flow>
                    <xsl:apply-templates select="par/point"/>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template match="point">
        <fo:block font-size="16pt" space-after="5mm">
            <xsl:apply-templates select="node()"/>
        </fo:block>
    </xsl:template>

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

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

</xsl:stylesheet>

given:

给出:

<doc>
    <par>
        <point>
            <text>some <bold>bold</bold></text>
        </point>
    </par>
    <par>
        <point>
            <text>some <italic>italic <bold>bolded</bold></italic></text>
        </point>
    </par>
</doc>

produces:

产生:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:page-sequence>
      <fo:flow>
         <fo:block font-size="16pt" space-after="5mm">
            some <fo:inline font-weight="bold">bold</fo:inline>
               </fo:block>
         <fo:block font-size="16pt" space-after="5mm">
            some <fo:inline font-style="italic">italic <fo:inline font-weight="bold">bolded</fo:inline>
            </fo:inline>
               </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>