Xslt 获取下面的第一页值
我有下面的XML文件Xslt 获取下面的第一页值,xslt,xslt-2.0,Xslt,Xslt 2.0,我有下面的XML文件 <?xml version="1.0" encoding="UTF-8"?> <root> <toc-item> <toc-title>A</toc-title> <page>11/4/10A</page> <page>cclxxi</page> </toc-item> <toc
<?xml version="1.0" encoding="UTF-8"?>
<root>
<toc-item>
<toc-title>A</toc-title>
<page>11/4/10A</page>
<page>cclxxi</page>
</toc-item>
<toc-item>
<toc-title>B</toc-title>
<page>11/5/1A</page>
</toc-item>
<toc-item>
<toc-title>C</toc-title>
<page>11/4</page>
<page>cclxxii</page>
</toc-item>
<toc-item>
<toc-title>B</toc-title>
<page>11/5/1A</page>
</toc-item>
<toc-item>
<toc-title>C</toc-title>
<page>11/4</page>
<page>cclxxiiv</page>
</toc-item>
</root>
A.
2010年4月11日
cclxxi
B
2011年5月11日
C
11/4
CCLxxi
B
2011年5月11日
C
11/4
CCLxxiv
在XSLT下面
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates></xsl:apply-templates>
</xsl:template>
<xsl:template match="root">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="toc-item">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="toc-title">
<td class="toc-title">
<xsl:apply-templates/>
</td>
</xsl:template>
<xsl:template match="content-style">
<xsl:variable name="fontStyle">
<xsl:value-of select="concat('font-style-',@font-style)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="./@font-style">
<span class="{$fontStyle}">
<xsl:apply-templates/>
</span>
</xsl:when>
<xsl:otherwise>
<span class="{concat('format-',./@format)}">
<xsl:apply-templates/>
</span>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="page">
<xsl:choose>
<xsl:when test="not(preceding-sibling::page[1])">
<td class="toc-page" valign="bottom">
<xsl:value-of select="."/>
</td>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="." mode="first"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="page" mode="first">
<xsl:variable name="pgn">
<xsl:value-of select="./following::page/following-sibling::page[1]"/>
</xsl:variable>
<xsl:processing-instruction name="pb">
<xsl:text>label='</xsl:text>
<xsl:value-of select="$pgn"/>
<xsl:text>'</xsl:text>
<xsl:text>?</xsl:text>
</xsl:processing-instruction>
<a name="{concat('pg_',$pgn)}"/>
</xsl:template>
<xsl:template match="page" mode="two">
<xsl:processing-instruction name="pb">
<xsl:text>label='</xsl:text>
<xsl:value-of select="."/>
<xsl:text>'</xsl:text>
<xsl:text>?</xsl:text>
</xsl:processing-instruction>
<a name="{concat('pg_',.)}"/>
</xsl:template>
</xsl:transform>
标签
'
?
请让我知道如何解决这个问题
谢谢我不确定我是否理解输出应该是什么样子,但是如果您更改
<xsl:template match="page" mode="first">
<xsl:variable name="pgn">
<xsl:value-of select="./following::page/following-sibling::page[1]"/>
...
...
进入
...
您将获得以下输出:
<td class="toc-title">A</td>
<td class="toc-page" valign="bottom">11/4/10A</td>
<?pb label='cclxxii'?><a name="pg_cclxxii"></a>
<td class="toc-title">B</td>
<td class="toc-page" valign="bottom">11/5/1A</td>
<td class="toc-title">C</td>
<td class="toc-page" valign="bottom">11/4</td>
<?pb label='cclxxiiv'?><a name="pg_cclxxiiv"></a>
<td class="toc-title">B</td>
<td class="toc-page" valign="bottom">11/5/1A</td>
<td class="toc-title">C</td>
<td class="toc-page" valign="bottom">11/4</td>
<?pb label=''?><a name="pg_"></a>
A
2010年4月11日
.
如评论中所述,没有可链接的后续页面的最后一个页面将有一个空链接。这可以通过以下调整来避免:
<xsl:template match="page" mode="first">
<xsl:variable name="pgn">
<xsl:value-of select="./following::page[preceding-sibling::page][3]"/>
</xsl:variable>
<xsl:if test="$pgn != ''">
<xsl:processing-instruction name="pb">
<xsl:text>label='</xsl:text>
<xsl:value-of select="$pgn"/>
<xsl:text>'</xsl:text>
<xsl:text>?</xsl:text>
</xsl:processing-instruction>
<a name="{concat('pg_',$pgn)}"/>
</xsl:if>
</xsl:template>
标签
'
?
我猜最后一个处理指令不应该被破坏?@MarcusRickert谢谢你的问题,因为我不确定OP想要什么作为最后一个没有进一步链接目标的页面的链接。我会在答案中加上这个,也许这会导致OP很快提出一个后续问题。嗨@matthias_h,你太棒了。这就是我要找的。再次感谢。:-)我认为您可以使用模式two
删除与内容样式
和页面
匹配的模板,因为它们当前未使用。您是否可以包括所需的输出(正如@mattias_h已经暗示的那样)?否则很难得出你的意图。既然您使用模式two
编写了匹配
的模板,那么您可能只想让模式one
和two
相应地处理
的第一次和第二次匹配?在这种情况下,您可以简单地扩展匹配条件,这将简化您的XSLT。请包括您想要的输出。
<xsl:template match="page" mode="first">
<xsl:variable name="pgn">
<xsl:value-of select="./following::page[preceding-sibling::page][3]"/>
</xsl:variable>
<xsl:if test="$pgn != ''">
<xsl:processing-instruction name="pb">
<xsl:text>label='</xsl:text>
<xsl:value-of select="$pgn"/>
<xsl:text>'</xsl:text>
<xsl:text>?</xsl:text>
</xsl:processing-instruction>
<a name="{concat('pg_',$pgn)}"/>
</xsl:if>
</xsl:template>
<td class="toc-title">C</td>
<td class="toc-page" valign="bottom">11/4</td>