Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mikkel Johansen 116 posts 292 karma points
    Jan 04, 2010 @ 22:29
    Mikkel Johansen
    0

    Generate anchors like <a name="X" />

    I am trying to generate some html anchors. But it ends up with some strange html code.

    XSLT

    <p class="header">
    TestHeader
    <a name="A" />
    </p>
    <p>TestBody</p>

    HTML

    <p class="header">
    TestHeader
    <a name="A"></a></p><p><a name="A">TestBody</a></p>

     

    Can anyone explain why there is an anchor around "TestBody" aswell ?

     

    - Mikkel

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 04, 2010 @ 22:36
    Chriztian Steinmeier
    0

    Hi Mikkel,

    The HTML you're showing seems to be a browser's internal view of the DOM (an anchor element is required by the spec to have a closing tag, so because you didn't close the a tag, the browser tries to guess what you intended by cloning the a tag).

    If you view the raw HTML source, my guess is you won't see the duplicated element(s).

    /Chriztian

  • Mikkel Johansen 116 posts 292 karma points
    Jan 04, 2010 @ 22:47
    Mikkel Johansen
    0

    Hi Chriztian

    Thanks for a very quick reply :-)

    Actually the dump is from Umbraco's "Visualize XSLT" with checkmark in "Encode/Decode result".

    And I did close the tag: <a name="A" />

    And I have tried with: <a name="A"></a>
    With the same effect.

    Correct there is a big different depending on the browser.

    Example from IE7, where the A tag is never closed even though I made the <a...></a>

    <P class=header>TestHeader <A name=A></P>
    <P>TestBody</P>

    I can accept that there is a browser issue (just like I am used to in CSS). But any clues on how to make it work ?

    -Mikkel

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 04, 2010 @ 22:47
    Chriztian Steinmeier
    0

    That said; If you're trying to generate anchors for specific places on a page (e.g., /page/#chapter01) - are you aware of the fact that any element with a uniquie id is reachable in that way?

    So instead of this (often seen) pattern:

    <p>
    <a name="chapter01"></a>
    ...
    </p>

    You can actually just do:

    <p id="chapter01">
    ...
    </p>

    and still get the link behavior you want?

    (It's also easier to generate with XSLT)

    /Chriztian

  • Mikkel Johansen 116 posts 292 karma points
    Jan 04, 2010 @ 22:52
    Mikkel Johansen
    0

    Great thanks :-) I will use the id, it will do the job.

    But it is still strange that the browsers is changing my html. BTW it works if I inserts a no-break-space in between the starting a-tag and end-tag.

    -Mikkel

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jan 04, 2010 @ 23:00
    Chriztian Steinmeier
    0

    Hi Mikkel,

    Oh boy - but it's definitely not XSLT generating the extra stuff.

    Yes, I saw you close the tag, but some tags aren't allowed to self-close like that. Problem is, in XML there's no difference between a self-closing tag like <a name="blah" /> and one with an explicit close tag if the tag is empty (<a name="blah"></a>), and XSLT outputs XML (when output method is set to "xml" - and setting that one to html opens another can of worms in the MSXML parser...)

    And yes, it'll work if you put something in the anchor tag so the XSLT processor doesn't self-close it - an nbsp will do (though it's technically illegal per spec, if I remember correctly) but I used to just put a comment in there:  

    <a name="coolNameHere"><xsl:comment /></a>

    But who's listening anyway now - go with the ids :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft