XSLT FO 列表元素
XSLT FO List elements
我的来源 XML 看起来像:
<events>
<entry>
<event>Event 1</event>
<event>Event 2</event>
<event>Event 3</event>
<event>Event 4</event>
</entry>
</events>
下面是我的XSL转换对应的代码:
<fo:block-container>
<fo:list-block>
<xsl:for-each select="//event">
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>
<xsl:value-of select="//event"/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:for-each>
</fo:list-block>
</fo:block-container>
FO输出:
<fo:list-block>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
我的问题是每个事件元素都应转换为单独的 fo:list 项,例如:
<fo:list-block>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 2</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 3</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
希望你能帮帮我...
而不是
<xsl:value-of select="//event"/>
使用
<xsl:value-of select="."/>
你要输出当前事件,毕竟不是全部
更笼统地说,我建议将您的 XSLT 程序从 <xsl:for-each>
更改为基于 <xsl:template>
/<xsl:apply-templates>
的形式:
<xsl:template match="events">
<fo:block-container>
<xsl:apply-templates />
</fo:block-container>
</xsl:template>
<xsl:template match="events/entry">
<fo:list-block>
<xsl:apply-templates />
<fo:list-block>
</xsl:template>
<xsl:template match="events/entry/event">
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
这种方法更加模块化,具有更好的可重用性,并且总体上没有那么深的嵌套。
将 <xsl:value-of select="//event"/>
替换为 <xsl:value-of select="."/>
,因为在 for-each
中,event
元素是上下文节点。
我的来源 XML 看起来像:
<events>
<entry>
<event>Event 1</event>
<event>Event 2</event>
<event>Event 3</event>
<event>Event 4</event>
</entry>
</events>
下面是我的XSL转换对应的代码:
<fo:block-container>
<fo:list-block>
<xsl:for-each select="//event">
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>
<xsl:value-of select="//event"/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:for-each>
</fo:list-block>
</fo:block-container>
FO输出:
<fo:list-block>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1Event 2Event 3Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
我的问题是每个事件元素都应转换为单独的 fo:list 项,例如:
<fo:list-block>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 1</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 2</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 3</fo:block>
</fo:list-item-body>
</fo:list-item>
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>Event 4</fo:block>
</fo:list-item-body>
</fo:list-item>
希望你能帮帮我...
而不是
<xsl:value-of select="//event"/>
使用
<xsl:value-of select="."/>
你要输出当前事件,毕竟不是全部
更笼统地说,我建议将您的 XSLT 程序从 <xsl:for-each>
更改为基于 <xsl:template>
/<xsl:apply-templates>
的形式:
<xsl:template match="events">
<fo:block-container>
<xsl:apply-templates />
</fo:block-container>
</xsl:template>
<xsl:template match="events/entry">
<fo:list-block>
<xsl:apply-templates />
<fo:list-block>
</xsl:template>
<xsl:template match="events/entry/event">
<fo:list-item>
<fo:list-item-label/>
<fo:list-item-body>
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
这种方法更加模块化,具有更好的可重用性,并且总体上没有那么深的嵌套。
将 <xsl:value-of select="//event"/>
替换为 <xsl:value-of select="."/>
,因为在 for-each
中,event
元素是上下文节点。