【问题标题】:xsl href not working in chromexsl href 在 chrome 中不起作用
【发布时间】:2012-02-06 12:06:52
【问题描述】:

我在我的 XSL 中使用了以下代码:

<xsl:variable name="link" select="normalize-space(concat('#',$chapter2))/>

<a href="{$link}">Next chapter</a>

点击下一章链接时,它应该导航到第 2 章的位置。 它没有导航到 chrome 和 firefox 的第 2 章。当我将鼠标悬停在链接上时,我发现在 chrome 和 firefox 中,在“#”之后添加了一些额外的字符,例如 #14678776e_chapter2。

如何解决这个问题。

$chapter2 是 position() 值 代码:

<xsl:variable name=chapter2 select="position()"/>

xsl 代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="w3.org/1999/XSL/Transform";
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        exclude-result-prefixes="msxsl"> 

    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/">
        <xsl:variable name="chapter" select="position()"/>
        <xsl:variable name="link" select="concat('#',$chapter)"/>
        <a href="{$link}" title="{$link}">
            <xsl:value-of select="$link"/>
        </a>
    </xsl:template>

</xsl:stylesheet>

暂时没有输入xml。以上代码可以直接运行

谢谢, 山姆

【问题讨论】:

  • 试试&lt;a href="{$link}" title="{$chapter2}"&gt;Next chapter&lt;/a&gt;。当鼠标提示 "14678776e_chapter2"(我敢打赌)时,检查 $chapter2 是如何创建的。
  • 是的,请向我们提供有关如何创建 $chapter2 的详细信息,我们可能会提供进一步的帮助。
  • @Tomalak - 鼠标提示显示第 2 章名称,但状态栏显示 14678776e_chapter2
  • @sam 请提供一个简短的 (!) XML 和 XSLT 示例来说明此问题。我不想要你的整个代码,写一个专门显示这个问题的示例。
  • @Tomalak 示例代码是:w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" >

标签: html firefox xslt google-chrome xslt-1.0


【解决方案1】:

名称“chapter2”需要用引号括起来:

<xsl:variable name=chapter2 select="position()"/>

另外,请确保定义第 2 章的模板与定义第 1 章的模板不同,否则第 1 章的变量绑定会影响第 2 章的变量绑定,因为两者都等于位置函数的结果。此代码工作正常:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="pitarget.xml"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
                >
<xsl:variable name="gal" select="'howdy'"/>
<?var gal?><!--howdy-->
<?echo gal?>
<?html5 ?>

<xsl:output method="html" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" />

<xsl:template match="xsl:stylesheet">
  <xsl:apply-templates select="processing-instruction()"/>
</xsl:template>

<xsl:template match="/">
  <xsl:variable name="chapter" select="position()"/>
  <xsl:variable name="link" select="concat('#',$chapter)"/>
  <xsl:value-of select="processing-instruction('html5')"/>
  <a href="{$link}" title="{$link}"><xsl:value-of select="$link"/></a>
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>


<xsl:template match="processing-instruction('echo')">
  <xsl:variable name="chapter2" select="position()"/>
  <xsl:variable name="link" select="normalize-space(concat('#',$chapter2))"/>
  <a href="{$link}">Next chapter</a>
  <xsl:value-of select="//xsl:variable/@select[../@name=current()]"/>
  <xsl:value-of select="count(document('pitarget.xml')//*) - 1"/>
</xsl:template>

<xsl:template match="processing-instruction('var')">
  <xsl:processing-instruction name="{.}">
    <xsl:value-of select="."/>
    <xsl:value-of select="./following-sibling::node()"/>
  </xsl:processing-instruction>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    相关资源
    最近更新 更多