ページ内のアンカー タグに移動する場合は、href属性が適切な値に設定された別のリンクが必要になります。たとえば、アンカータグが次の場合:
<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 をコーディングする 1 つの方法は、 people要素に一致する 2 つのテンプレートを用意し、それらを区別するためのモード属性を使用することです。
たとえば、この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>
person要素を一致させるために 2 つの別個のテンプレートを使用することを避けたい場合は、テンプレートにパラメーターを渡して、上と下を区別することができます。この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>