html页面上的XSL超链接
我正在学习XSL,我对在一个HTML文件中生成双向超链接有疑问 例如,我们有html页面上的XSL超链接,html,xml,xslt,hyperlink,Html,Xml,Xslt,Hyperlink,我正在学习XSL,我对在一个HTML文件中生成双向超链接有疑问 例如,我们有 <person id="first"> -<name>Bob</name> -<age>19<age> <person id="second"> -<name>smith</name> -<age>12<age> <person id="third"> -<name>Lisa&
<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>
-鲍勃
-19
-史密斯
-12
-丽莎
-30
在XML文件中,我想用XSLT在一个HTML页面上创建3个超链接
例如,在HTML页面的顶部,我们有三个链接:
然而,它并没有真正起作用
有人能举个例子吗
谢谢。如果您想导航到页面中的锚定标记,您需要另一个链接,该链接的href属性设置为适当的值。例如,如果锚定标记为:
<a id="first">Bob</a>
在您的情况下,您希望锚定彼此链接,因此两个a元素都具有id和href
<a id="first_top" href="#first_bottom">Bob</a>
<a id="first_bottom" href="#first_top">Bob</a>
编写XSLT代码的一种方法是使用两个模板匹配people元素,但使用mode属性来区分它们
例如,试试这个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,并且所有标记都已关闭)
中间有一些内容
如果希望避免使用两个单独的模板来匹配person元素,可以将参数传递给模板以区分顶部和底部。这个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>
中间内容
如果要导航到页面中的锚定标记,则需要将href属性设置为适当值的另一个链接。例如,如果锚定标记为:
<a id="first">Bob</a>
在您的情况下,您希望锚定彼此链接,因此两个a元素都具有id和href
<a id="first_top" href="#first_bottom">Bob</a>
<a id="first_bottom" href="#first_top">Bob</a>
编写XSLT代码的一种方法是使用两个模板匹配people元素,但使用mode属性来区分它们
例如,试试这个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,并且所有标记都已关闭)
中间有一些内容
如果希望避免使用两个单独的模板来匹配person元素,可以将参数传递给模板以区分顶部和底部。这个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>
中间内容
这不一定是XSLT问题,您只需生成适当的
,反之亦然。例如,顶部链接可以是
<xsl:for-each select="person">
<a id="top_{@id}" href="#bottom_{@id}">
<xsl:value-of select="name"/>
</a>
</xsl:for-each>
这不一定是XSLT问题,您只需生成适当的
,反之亦然。例如,顶部链接可以是
<xsl:for-each select="person">
<a id="top_{@id}" href="#bottom_{@id}">
<xsl:value-of select="name"/>
</a>
</xsl:for-each>
如果a)输入的XML格式正确,b)以HTML格式给出预期输出,而不是(或除此之外)对其进行解释,那么您获得答案的几率会高得多,c)将当前XSLT样式表添加到问题中。如果a)输入XML格式正确,b)以HTML格式给出预期输出,而不是(或另外)解释它,以及c)将当前XSLT样式表添加到问题中,则获得答案的几率将大得多。