2013-02-22 105 views
0

我正在学习XSL,并且有关于在一个HTML文件中生成双向超链接的问题。HTML页面上的XSL超链接

例如,我们有

<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一个HTML页面上创建超链接3。

例如,在HTML页面的顶部,我们有三个环节:

  1. 鲍勃
  2. 史密斯
  3. 丽莎

而就在同HTML页面的底部,我们有三个链接:

  1. Bob
  2. 史密斯
  3. 丽莎

如果用户点击鲍勃,我们去鲍勃页面(1的底部。鲍勃< - >鲍勃)

如果用户点击鲍勃,我们去鲍勃在页面的底部

如果用户点击2.史密斯,我们去5·史密斯底部页面(5史密斯< - > 5.Smith)

如果用户点击5.史密斯,我们去2.史密斯页面底部

我试图用<a id="some value"> </a>

然而,它并没有真正的工作。

任何人都可以举个例子吗?

谢谢。

+1

如果您a)输入XML格式良好,您将有更高的机会获得答案,b)您以HTML格式而非(或者除此之外)解释它,以及c)将当前的XSLT样式表添加到您的问题中。 – 2013-02-22 08:35:11

回答

3

如果要导航到页面中的定位标记,则需要使用另一个链接,并将该属性设置为适当的值。例如,如果锚标记是这样的:

<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要做到这一点的方法之一是有两个模板匹配元素,但有模式属性来区分它们

试试这个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> 
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> 
+0

感谢它真的帮助我理解! – 2013-02-22 08:41:53