|
|
 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |
|

June 22nd, 2009, 09:08 AM
|
|
Authorized User
|
|
Join Date: Sep 2008
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
General XSLT query
Hi
I am completely new to XML and XSL and now using an application call site publisher by Teamsite interwoven. I have managed to understand some basic XSL concepts but struggling with a problem I cannot seem to get my head round.
The left navigation from our site pulls in the links from a sitemap file which works great. The left nav has 3 levels of navigation and I am trying to create a simple xsl component to a content page which also reads in the site map links but what I am trying to do is:
Say I have 3 main links and 3 sublinks
Level one
Level one a
Level one b
Level one c
Level two
Level two a
etc. Whilst in level one, I have a link in the main content of the page at the footer with a link to level one a and so on. When I am on page level one c, I want the footer link to not to goto level two but to a seperate page.
So I would like to loop through the links and determine the page i'm on using a current page variable and do a comparison. When I get the match, I would like to check to see if there is a link after this, if so, show the next page link url, otherwise, goto a generic page (ie, hardcoded link).
Sorry if a bit vague but tried to explain in english.
Some code I have is:
<!-- get current page -->
<xsl:variable name "currpage">
<xsl:value-of select="/pagelocation/page_info/@pagename">
</xsl:variable>
Then to loop through the sitemap (3 altogher - one loop for each level of sitemap:-
<xsl:for-each select = "node[@visible-in-sitemap='true']">
Do some code in here. Currently just display value-of select="link/value"
</xsl:for-each>
Thank you to anyone who can help me. Maybe there is some easy syntax to do this but I do not know XSL much at all.
|

June 22nd, 2009, 09:23 AM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
Rather than describing your problem in terms of the user experience, it's much easier for us if you show the XML input and HTML output of your transformation, simplified as much as possible to remove irrelevancies.
Your code fragments mean nothing in the absence of a source document.
By the way
Code:
<xsl:variable name "currpage">
<xsl:value-of select="/pagelocation/page_info/@pagename">
</xsl:variable>
is a verbose and inefficient (and sometimes incorrect) way of writing
Code:
<xsl:variable name "currpage" select="/pagelocation/page_info/@pagename"/>
Get out of the habit before it's too late.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|

June 22nd, 2009, 10:58 AM
|
|
Authorized User
|
|
Join Date: Sep 2008
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi
Thank you for your quick reply. this is where I am stuck. The application I am using is Teamsite which I create the nav via a standard component within the web interface (hence not being able to see the xml it creates).
I can create my own components and got the sitemap xsl data from an xsl template in another component so can see the generated code to display the site map from the code I supplied.
I think my main question is knowing how to use xsl syntax to grab the current page url into a variable, then whilst in the loop, check to see if the loop variable is the same as the current page, if it is, then get the next url in the loop to use as a link. but then if it is the last page, no need to get the next url and and either leave blank or hard code a link.
I think it is just the xsl syntax I need to know. If anyone could give me a code example of how I could go about this, it will help millions. I could then try and translate into what I have got to work with.
Many thanks again.
|

June 22nd, 2009, 11:01 AM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
Sorry, but without knowing what the XML input looks like, writing XSLT to process it is impossible.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|

June 22nd, 2009, 11:12 AM
|
|
Authorized User
|
|
Join Date: Sep 2008
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Would it be possible to make a dummy input xml just so I can understand?
for example:
<code>
<sitemap>
<navlink>
<linkname>some link one</linkname>
<linkpath>http//www.linkone.com</linkpath>
</navlink>
<navlink>
<linkname>some link two</linkname>
<linkpath>http//www.linktwo.com</linkpath>
</navlink>
<navlink>
<linkname>some link three</linkname>
<linkpath>http//www.linkthree.com</linkpath>
</navlink>
</sitemap>
</code>
Then I would use the <xsl: template match="sitemap"> and use the for-each select to do the code?
Or have I totally lost it?
|

June 22nd, 2009, 11:22 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 Posts
|
|
XSLT does not have loops. I am also not sure what you expect the "current page url" to be. You can however pass parameters to an XSLT stylesheet thus you can define a global parameter
Code:
<xsl:param name="cp"/>
in your stylesheet, set it with the API your XSLT processor exposes to your environment, pass in what you regard as the current page url and then use that parameter in XPath expressions e.g.
Code:
<xsl:template match="sitemap">
<xsl:value-of select="navlink[linkpath = $cp]/following-sibling::navlink[1]/linkpath"/>
</xsl:template>
will output the linkpath of the first navlink sibling of that navlink element where linkpath is equal to the parameter.
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|

June 22nd, 2009, 12:10 PM
|
|
Authorized User
|
|
Join Date: Sep 2008
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Aah. I assume I am using Xpath then. Here is the code cut n paste which I have so far. My code just displays all the left nav links from the sitemap at the moment but you can see how it is generated using a loop.
I guess if you cannot help me from this code then i'm stuck and will have to go back to the teamsite supplier for help. I assumed it would be a general XSL question but guess it is a bit more difficult than I first thought. I hate being a newby to stuff like this.
Here is the code pasted straight from the app...
this gets the sitemap info
Code:
<Data>
<External>
<Object Scope="local">com.interwoven.livesite.external.impl.LivesiteSiteMap</Object>
<Method>getSiteMap</Method>
</External>
</Data>
and this does the loop and displays the links
Code:
<!DOCTYPE html-entities SYSTEM "http://www.interwoven.com/livesite/xsl/xsl-html.dtd">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Skin: Default (Default Browser Type) -->
<xsl:include href="http://www.interwoven.com/livesite/xsl/HTMLTemplates.xsl"/>
<xsl:include href="http://www.interwoven.com/livesite/xsl/StringTemplates.xsl"/>
<!-- FOR TESTING -->
<xsl:variable name="currPage">investments/individual_saving_accounts/how_to_apply</xsl:variable>
<!-- USE THIS WHEN READY TO GO LIVE
<xsl:variable name="currPage">
<xsl:value-of select="/Properties/Data/Result/page_info/@pagename" />
</xsl:variable>
-->
<xsl:template match="/">
<xsl:apply-templates select="/Properties/Data/Result/site-map/segment"/>
</xsl:template>
<xsl:template match="segment">
<div class="">
<xsl:for-each select="node[@visible-in-sitemap='true']">
<ul class="sitemapLevel1">
<li>LEVEL1: - <xsl:value-of select="link/value"/></li>
<xsl:for-each select="node[@visible-in-sitemap='true']">
<ul class="sitemapLevel2">
<li> LEVEL2: - <xsl:value-of select="link/value"/> </li>
<xsl:for-each select="node[@visible-in-sitemap='true']">
<ul class="sitemapLevel3">
<li> -LEVEL3: - <xsl:value-of select="link/value"/> </li>
</ul>
</xsl:for-each>
</ul>
</xsl:for-each>
</ul><br/>
</xsl:for-each>
</div>
<!-- <p class="next"><a href="#"><xsl:value-of select="$currPage"/></a></p> -->
<p class="next"><a href="#"><xsl:value-of select="$currPage"/></a></p>
</xsl:template>
<xsl:template name="navLink">
<!-- output node as a link -->
<a>
<xsl:attribute name="href">
<xsl:value-of select="link/value"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="description"/>
</xsl:attribute>
<xsl:value-of select="label"/>
</a>
</xsl:template>
</xsl:stylesheet>
It is difficult to show you the outputted style text but the output in text form is:
LEVEL1: - index
LEVEL1: -
LEVEL2: - linkhere
LEVEL2: - linkhere
-LEVEL3: - linkhere
LEVEL2: - linkhere
LEVEL2: - linkhere
LEVEL1: - linkhere
LEVEL1: - linkhere
LEVEL1: - linkhere
LEVEL2: - linkhere
-LEVEL3: - linkhere
-LEVEL3: - linkhere
-LEVEL3: - linkhere
-LEVEL3: - linkhere
-LEVEL3: - linkhere
LEVEL2: - linkhere
-LEVEL3: - linkhere
-LEVEL3: - linkhere
LEVEL1: - linkhere
LEVEL1: - linkhere
LEVEL1: - linkhere
LEVEL2: - linkhere
actual href link here (just for testing of the style but will be in the loop)
|

June 22nd, 2009, 12:31 PM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
I don't think that writing the XSLT code will be at all difficult if you can only find a way to tell us clearly what its inputs and outputs are.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|

June 22nd, 2009, 01:30 PM
|
|
Authorized User
|
|
Join Date: Sep 2008
Location: , , .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Maybe if I could get some help with some imaginery input and outputs? then I could try and work out how to get the real ones later? At this stage, it would be good to know how the syntax would work? Once I can do that, I could translate later?
|

June 22nd, 2009, 01:46 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 Posts
|
|
Then provide the imaginery input and the corresponding output you want the stylesheet to create. You did already provide some input however and I posted an XPath expression to find a certain node in that input.
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |