0

XSL を勉強していますが、1 つの HTML ファイルで双方向のハイパーリンクを生成することについて質問があります。

たとえば、

<person id="first">
-<name>Bob</name>
-<age>19<age>
<person id="second">
-<name>smith</name>
-<age>12<age>
<person id="third">
-<name>Lisa</name>
-<age>30<age>

XML ファイルで、XSLT を使用して 1 つの HTML ページに 3 つのハイパーリンクを作成したいと考えています。

たとえば、HTML ページの上部には、次の 3 つのリンクがあります。

  1. ボブ
  2. スミス
  3. リサ

同じ HTML ページの下部に、次の 3 つのリンクがあります。

  1. ボブ
  2. スミス
  3. リサ

ユーザーが 1.Bob をクリックすると、ページ下部の 4.Bob に移動します (1.Bob <-> 4.Bob)。

ユーザーが 4.Bob をクリックすると、ページ下部の 1.Bob に移動します

ユーザーが 2.Smith をクリックすると、ページの下部にある 5.Smith に移動します (5.Smith <-> 5.Smith)

ユーザーが 5. Smith をクリックすると、ページの下部にある 2. Smith に移動します

使ってみた<a id="some value"> </a>

しかし、実際にはうまくいきませんでした。

誰でも例を挙げることができますか??

ありがとう。

4

2 に答える 2

3

ページ内のアンカー タグに移動する場合は、href属性が適切な値に設定された別のリンクが必要になります。たとえば、アンカータグが次の場合:

<a id="first">Bob</a>

次に、リンクは次のようになります

<a href="#first">Bob</a>

あなたの場合、アンカーを相互にリンクする必要があるため、両方要素にidhrefの両方が含まれます

<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>
于 2013-02-22T08:38:30.343 に答える
1

これは必ずしも XSLT の質問ではありません。適切な質問を生成する必要があるだけで、<a id="link1" href="#link4">...</a>その逆も同様です。たとえば、トップ リンクは次のようになります。

<xsl:for-each select="person">
  <a id="top_{@id}" href="#bottom_{@id}">
    <xsl:value-of select="name"/>
  </a>
</xsl:for-each>
于 2013-02-22T08:41:23.583 に答える