如果要导航到页面中的定位标记,则需要使用另一个链接,并将该属性设置为适当的值。例如,如果锚标记是这样的:
<a id="first">Bob</a>
那么你的链接会是这样
<a href="#first">Bob</a>
在你的情况,你想锚链接到对方,所以双方一个元素会既有ID和HREF
<a id="first_top" href="#first_bottom">Bob</a>
<a id="first_bottom" href="#first_top">Bob</a>
编写你的XSLT要做到这一点的方法之一是有两个模板匹配人元素,但有模式属性来区分它们
试试这个XSLT例如
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/people">
<html>
<body>
<xsl:apply-templates select="person" mode="top"/>
<p>
Some content in the middle
</p>
<xsl:apply-templates select="person" mode="bottom"/>
</body>
</html>
</xsl:template>
<xsl:template match="person" mode="top">
<p>
<a id="{@id}_top" href="#{@id}_bottom">
<xsl:value-of select="name" />
</a>
</p>
</xsl:template>
<xsl:template match="person" mode="bottom">
<p>
<a id="{@id}_bottom" href="#{@id}_top">
<xsl:value-of select="name" />
</a>
</p>
</xsl:template>
</xsl:stylesheet>
该输出以下(假设你有一个根元素良好的XML,以及所有标记关闭)
<html>
<body>
<p><a id="first_top" href="#first_bottom">Bob</a></p>
<p><a id="second_top" href="#second_bottom">smith</a></p>
<p><a id="third_top" href="#third_bottom">Lisa</a></p>
<p>Some content in the middle</p>
<p><a id="first_bottom" href="#first_top">Bob</a></p>
<p><a id="second_bottom" href="#second_top">smith</a></p>
<p><a id="third_bottom" href="#third_top">Lisa</a></p>
</body>
</html>
如果要避免具有两个分离templa如果匹配个人元素,则可以将参数传递给模板,以区分顶部和底部。这XSLT也将工作
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/people">
<html>
<body>
<xsl:apply-templates select="person">
<xsl:with-param name="idpos" select="'top'" />
<xsl:with-param name="hrefpos" select="'bottom'" />
</xsl:apply-templates>
<p>
Some content in the middle
</p>
<xsl:apply-templates select="person">
<xsl:with-param name="idpos" select="'bottom'" />
<xsl:with-param name="hrefpos" select="'top'" />
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="person">
<xsl:param name="idpos" />
<xsl:param name="hrefpos" />
<p>
<a id="{@id}_{$idpos}" href="#{@id}_{$hrefpos}">
<xsl:value-of select="name" />
</a>
</p>
</xsl:template>
</xsl:stylesheet>
如果您a)输入XML格式良好,您将有更高的机会获得答案,b)您以HTML格式而非(或者除此之外)解释它,以及c)将当前的XSLT样式表添加到您的问题中。 – 2013-02-22 08:35:11