简体   繁体   中英

xsl href not working in chrome

I have used the following code in my XSL:

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

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

It should navigate to chapter 2 position on click of the next chapter link. Its not navigating to chapter 2 in chrome and firefox. When I hover to the link I found that in chrome and firefox after '#' some extra characters gets added like #14678776e_chapter2.

How to fix this issue.

$chapter2 is position() value Code:

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

Code for 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>

There is no input xml for now. The above code can be run directly

Thanks, Sam

The name "chapter2" needs to be surrounded by quotes:

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

Also, make sure the template that defines chapter2 is not the same that defines chapter1, or else the variable binding of chapter1 will shadow the variable binding of chapter2, since both will equal the result of the position function. This code works fine:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM