我的源 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-item,例如:
<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>
这种方法更加模块化,具有更好的可重用性,并且总体上嵌套不那么深。
关于xml - XSLT FO 列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32166538/