Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / XML / XSL guru out there? Problem...

Author
Message
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Mar 2013 15:19 Edited at: 21st Mar 2013 15:20
Amazingly, this question didn't get answered on CodeProject. Can anyone help, or know the best site to get an answer on this one?
=====

I am truly scratching my head with this one. I would like to create a bulleted list with hyperlinks from an XML excerpt. I can create the bulleted list, but I can't work out how to get the hyperlink part together.

Here is the XML:



...and the XSL so far. The hyperlink components do not work. It may look a bit untidy through my attempts to make it work. I would like the "Go to Page n" to be the hyperlink.




The current result is:

1. HREF='page1.xml' Go to Page 1
2. HREF='page2.xml' Go to Page 2
3. HREF='page3.xml' Go to Page 3
4. HREF='page4.xml' Go to Page 4


Any help appreciated!

Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 21st Mar 2013 16:53 Edited at: 21st Mar 2013 16:54
The HREF part should be an attribute of the <a> tag. Should read like this: <a href='page.xml'>Caption Text</a>.

Also href should be lowercase to conform with XHTML.

I cannot remember how you output XML attributes with XSL however.

Jimpo
21
Years of Service
User Offline
Joined: 9th Apr 2005
Location:
Posted: 21st Mar 2013 17:10 Edited at: 21st Mar 2013 17:14
I used to be able to help with this, but the second I no longer needed to know XSL, I forgot everything about it. Shows how good I am at remembering things

Quote: "Can anyone help, or know the best site to get an answer on this one?"

stackoverflow.com is the go to place for coding questions. I'm always referencing it, whether it's for work, class, or personal projects. They have answers for everything!

Edit:Haha after rereading this, the previous sentence sounds like one of those cheesy advertisements. I am not paid to endorse stack overflow, I swear!

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Mar 2013 19:20
Quote: "The HREF part should be an attribute of the <a> tag. Should read like this: <a href='page.xml'>Caption Text</a>"


Yes that's what I want and am having trouble defining in the XSL. I've added it to the description of my problem and posted on Stackoverflow many thanks

RedFlames
18
Years of Service
User Offline
Joined: 25th Aug 2007
Location: Germania
Posted: 21st Mar 2013 19:21 Edited at: 21st Mar 2013 19:26
Okay, just ran this through Java... haven't done any programming in over half a year and Java was the last thing I used, even did some brief XML file reading magic back then, but nothing that would help much here...

So I went off to google and clicked the first results I found for some examples about Java XSL Transforms and about XSLT itself. Unfortunately both of these are german. But I bet resources like this exist in english as well.
Link 1
Link 2

So I set up my little Transformer () in Eclipse and let him do the magic. First off he complained that there was no closing "</xsl:stylesheet>" tag but I assume you just forgot to copy&paste that.

Here's the initial output for me:


...something is very wrong there. He did not even output url or name. I can only assume it's due to the way you adressed "hyperlink/url" with the slash in it and my parser is to stupid to read that or whatever. I dunno if that is correct XSL or not, but was too lazy to find out.

Anyways. Here's the version I got that outputs the stuff you want: (atleast for me)


The important part is:
Quote: "<A><xsl:attribute name="href"><xsl:value-of select="url" /></xsl:attribute>"

this creates the anchor tag and assigns the href-attribute to it. Which will result in the output we want:
Quote: "<A href=" url goes here ">"


However with the way the xml document is structured I had to leave the template for "url" in there and leave it empty. This is important, because this way the url will not be copied as plaintext, which it would if you told it to 'apply-templates' or if you left the template out.

For the name you could safely remove the template, and the 'name'-tag in the XML-doc too - unless you need this later on.

Oh and I cut out the DIV-tag around the individual list elements, if you still need that one for some reason, put it back in.

Anyways, from my very brief experience with XSL (I'm using this for the first time pretty much ) I think this XSL doc is much nicer:


...or to eliminate the empty url template, even shorter version:


Oh and notice how we do have the slashes in the select command, and these didn't work for me in the beginning? I assume these are based on where you are in the hierarchy. When you were applying templates to the inside of hyperlink (oh btw I scrapped that one out of the XML doc... it was basically interchangeable with 'listitem' and could be omitted as it had no real function) and... hm... well looking at the initial XSL document maybe from the apply-templates in the listitem it could potentially apply the hperlink template (which just pastes the content into the <a>-tag) OR it could apply the match="hyperlink/url" and match="hyperlink/name"? Would that be the way this works? Maybe these conflict somehow and in your Parser it is applying the latter two, but in mine the 'hyperlink'-template already eliminated the url and name.
but as I said I have no idea what I'm doing, I just happen to get it to work through the Magic of Friendship™

[and just for clarification, my current XML doc for the last XSL file would be without the hyperlink tags:
]

~RedFlames
Edit: Typos everywhere... I hope I killed 'em all. Also, this post is fomatted pretty badly. I won't fix that. :I
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 21st Mar 2013 19:39
Thanks for looking at this, and for the detailed explanation. It has helped me understand XSL transformations much better Now I see the importance of attribute, and I see how value-of actually works. I searched sites like w3schools.com but couldn't find answers to anything related to attributes (although I thought they were called elements).

I have tried to put this back into the bigger XML document, I have extracted the part that was causing me a problem. So now, I have to isolate the list in the template match, I can't just use "/". I have tried to use "list" but then it does not display the listitems at all



So then I changed this line:

<xsl:for-each select="list/listitem">

to

<xsl:for-each select="listitem">

Now I get the bullets, but they do not have any text

I am close but not close enough Can you help with this last component?

RedFlames
18
Years of Service
User Offline
Joined: 25th Aug 2007
Location: Germania
Posted: 21st Mar 2013 19:41
No text in them, hmm... did you scratch the hyperlink tags yet? Because the XSL assumes that url and name are direct child-tags (or whatever that is called) of listitem. If the hyperlink is surrounding them it won't find them. If this is not the problem, then I don't know.
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 22nd Mar 2013 06:29
I have fixed it. This line:

<xsl:attribute name="href"><xsl:value-of select="url" />

should read:

<xsl:attribute name="hyperlink/href"><xsl:value-of select="hyperlink/url" />

Many thanks for your help, I've learned far more than just the question I asked!

Login to post a reply

Server time is: 2026-07-16 00:30:37
Your offset time is: 2026-07-16 00:30:37