*
The moose likes XML and Related Technologies and the fly likes xsl: anchor problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xsl: anchor problem" Watch "xsl: anchor problem" New topic
Author

xsl: anchor problem

Tom Brown
Greenhorn

Joined: Sep 25, 2003
Posts: 22
Hey
I've got a problem with anchors. I link in a page to an other page. I get an XML page styled with the same xsl stylesheet. Sometimes the link refers to a page, and sometimes it refers to a page and an anchor in that page, so i need to go to that anchor in the page. I can't solve it like you can do it in html.
How do i make it possible to go to the anchor that i pass with the url?
thanks a lot?
xml:
---
<INT-REF ID="4:35">4:35</INT-REF>
<ANCHOR ID="1" /> further in the page
xsl-stylesheet:
--------------
<xsl:template match="INT-REF">
<b><a>
<xsl:attribute name="href">
/cvweb.xar?VIEW=<xsl:value-of select="@ID"/>&ANCHOR=<xsl:value-of select="@ID"/>
</xsl:attribute>
<xsl:attribute name="target">mainframe</xsl:attribute>
<xsl:apply-templates/>
</a></b>
</xsl:template>
<xsl:template match="ANCHOR">
<a>
<xsl:attribute name="name">
<xsl:value-of select="@ID"/>
</xsl:attribute>
</a>
</xsl:template>
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Tom Brown:
I can't solve it like you can do it in html.
Why?
If you can't construct the URL with "#myanchor" in it, then would it be possible to make the "cvweb.xar" page include an "onload" attribute for the BODY element which would refresh the page to "#myanchor"?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Tom Brown
Greenhorn

Joined: Sep 25, 2003
Posts: 22
hey lasse,
i can't do it with the url anchor. i can give the anchor attribute to the destination xml file, but how can i implement the onload feature in the xml file?
i've searched on it, with no good result
maybe i can try to do it in javascript
thx
tom
[ March 31, 2004: Message edited by: Tom Brown ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The JavaScript way is exactly what I meant.
I assume you're creating an HTML page to be displayed in a web browser, right? And this page eventually has a link pointing to "/cvweb.xar?VIEW=edit&ANCHOR=myanchor", right? Now, if the "cvweb.xar" component (what technology is that, by the way?) is going to generate an HTML page with <a name="myanchor"> somewhere on it, you could make your server-side code add an onload=makeTheBrowserGoToAnchor('myanchor') attribute to the HTML <body> tag. The outcome should be that once "cvweb.xar" has generated the HTML page, the browser loads it without #myanchor in the URL. Now, because the <body> tag has an onload attribute, it executes whatever JavaScript it has. In your case, the JavaScript function makes a simple refresh operation with the URL "#myanchor".
Tom Brown
Greenhorn

Joined: Sep 25, 2003
Posts: 22
hey lasse,
this is the xml code that i get after the request.
http://cvweb.xar?VIEW=<xsl:value-of select="@ID"/>&ANCHOR=<xsl:value-of select="@ANCHOR"/>
so i get the right article 4:12 with a reference to the anchor... see in the TREE -> anchor="2".
i've tried to get the attribute anchor in the body onload but it give some errors. how can i get the anchor attribute from the TREE in the javascript?
thanks a lot lasse... i think it isnt that difficult, but i did't find it yet.
xml after request..
-------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="xsl/one.xsl"?>
<TREE ANCHOR="2">
<MARKUP>Art. 4:12</MARKUP>
<KNR><ANCHOR ID="1" />1</KNR>blablabla...
<INT-REF ID="4:7" ANCHOR="4"> 4:7</INT-REF>
<L1>
<KNR><ANCHOR ID="2" />2</KNR>
<L1>....
one.xsl
-------
<script language="JavaScript" type="text/JavaScript">
<!--
function goToSection(sectionName)
{
location.hash = sectionName;
}
//-->
</script>
</head>
<xsl:for-each select="//TREE">
<body leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0" bgcolor="#ffffff" onLLoad="goToHash('<xsl:value-of select="@ANCHOR"/>');">
</xsl:for-each>
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Is "onLLoad" a typo in your post or in your XSL?
Tom Brown
Greenhorn

Joined: Sep 25, 2003
Posts: 22
hey lasse
its the html onload thing that i use in my xsl stylesheet.
he gives an error on the goToHash('<xsl:value-of select="@ANCHOR"/>') between the javascript function
do i have to work with a variable? like <xsl:variable name="anchor">?
i've tried this:
<xsl:for-each select="TREE">
<xsl:variable name="anker">
<xsl:value-of select="@ANCHOR"/>
</xsl:variable>
</xsl:for-each>
<script language="JavaScript" type="text/JavaScript">
<![CDATA[
function goToSection(sectionName)
{
location.hash = sectionName;
}
]]>
</script>
</head>
<body leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0" bgcolor="#ffffff" onlload="goToHash('$anker');">
-------------------------------------------------
but i get $anker instead of the anchor itself...
is the variable not well formed? i dont have experience with variables
thanks a lot
tom
---------------------
tried this also.. no result...
------------------------------
<script language="javascript">
<![CDATA[
function goToHash() {
location.hash = ']]><xsl:for-each select="//TREE"><xsl:value-of select="@ANCHOR"/></xsl:for-each><![CDATA[';
}
]]>
</script>
</head>
<body leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0" bgcolor="#ffffff" onlllload="goToHash();">
[ April 01, 2004: Message edited by: Tom Brown ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hmm. The variables are nasty. For example, you can only set a value for a variable once. I'll try to squeeze in some time to play around with this anchor-creating stuff and see if I can make it work. No promises, though since I also hate XSL (except when everything works as expected)
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ok. For the following XML file (generated by your server-side code, I believe)

the following XSL stylesheet

produces the following HTML

Is this what you're after?
 
Don't get me started about those stupid light bulbs.
 
subject: xsl: anchor problem
 
Similar Threads
XSL Problem
Insert JPEGs into XML
XSL fails with xmlns
xsl: Send/remember xml to show in other frame/xsl-stylesheet
[XSL-FO][docbook]Problem print line in table of contents