Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 25th, 2006, 09:39 AM
Authorized User
 
Join Date: May 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to boen_robot
Default Using document() with relative paths

Hello. I seem to be having trouble with an XSLT based menu I'm constructing.

I have a main XSLT which includes another one for the menu. The data for the menu is in it's own XML file. That said, I need to use the document() function in order to transform the menu data and still transform the main data. So far, so good. But for some reason the function isn't working as I though it would.

The important part of the main XSLT:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="navigation.xsl"/>
<xsl:template match="/">
...
<div id="left">
<xsl:call-template name="navigation"/>
</div>
...
</xsl:template>
</xsl:stylesheet>
Where navigation.xsl is
Code:
<?xml version="1.0" encoding="windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="Nav" select="'navigation.xml'"/>
<xsl:template name="navigation">
<xsl:for-each select="document($Nav,/)">
<xsl:call-template name="menu" />
</xsl:for-each>
</xsl:template>

<xsl:template name="menu">
<xsl:for-each select="document($Nav,menu)">[list]
    <xsl:for-each select="document($Nav,item)">
    <li>
    <a href="{document($Nav,link)}">
        <xsl:value-of select="document($Nav,menu/item/title)"/>
    </a>
    </li>
    <xsl:if test="document($Nav,menu)">
        <xsl:call-template select="menu"/>
    </xsl:if>
    </xsl:for-each>
</ul>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And the problematic XML for which I need the document function.
Code:
<?xml version="1.0" encoding="windows-1251"?>
<menu>
    <item>
        <title>Home</title>
        <link>#</link>
    </item>
    <item>
        <title>Products</title>
        <link>#</link>
        <menu>

        </menu>
    </item>
</menu>
The reason I'm having a template navigation to call the second template menu is that menu is suppose to be repeated for every nested menu.

The problem is that for some reason, every document() function below
Code:
<xsl:for-each select="document($Nav,menu)">
is treated as a relative path to the root of the document, not the previously selected by this for-each path (in this case- menu). It gets worse. Because of call-template which selects itself and because the root <menu> would always exist in the whole file, the template is being looped infinetly, thus eventually causing IE to crash. At the same time, FF is being wise enough to not actually select anything below
Code:
<xsl:for-each select="document($Nav,menu)">
My desired output is something like this:
Code:
[list]
    <li>
        <a href="#>Home</a>
    </li>
    <li>
        <a href="#">Products</title>
        [list]

        </ul>
    </li>
</ul>
Any ideas?
__________________
------
&lt;xsl:for-each select=\"problem\"&gt;&lt;xsl:value-of select=\"solution\"&gt;&lt;/xsl:for-each&gt;
 
Old May 28th, 2006, 12:00 PM
Authorized User
 
Join Date: May 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to boen_robot
Default

Let me paraprase my question...

How do you process two XML documents simultaneously?

I mean... this would have worked if I was only transforming the navigation.xml file, but the problem is I have to transform the per-page data and add the menu at the same time.

------
<xsl:for-each select="problem"><xsl:value-of select="solution"></xsl:for-each>
 
Old May 28th, 2006, 01:29 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm afraid I really don't understand what your code is trying to achieve. I think you have misunderstood the purpose of the second argument to the document() function, and that when you write document($Nav, x/y/z), you should be writing document($Nav)/x/y/z

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 29th, 2006, 07:24 AM
Authorized User
 
Join Date: May 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to boen_robot
Default

What my code is trying to achieve is what I show in the desired output (the last code). The problem is that I'm actually transforming a variety of XML files, and this menu has to be applyed on all of them. The menu's data (titles, links, maybe something else) however is in another XML, instead of being embeded in the XSLT or something, so it should be processed simultaniously with the main document.

My problem is practically that I need to somehow use relative paths on the external document, or perhaps another approach. The reason is that I don't know how deep the menu is going to be, so the same "menu to ul", "link to a" and "title to <a>'s content" transformations should be applyed for each nested list.

Up until the moment I read your post, using things after the document() function always resulted in some sort of errors. I don't know. But it worked when I tryed it then, so I guess I'll be sticking with it...

If the XPath expression for inside the document is suppose to follow after the document() fucntion, then what's it's second argument for anyway? I saw the specification, but I can't grasp what unions or whatever they are talking about.

I tryed experimenting with having different second argument and a following XPath expression, but I still don't get even near to what I'm after.

Oh, and thanks for the reply by the way.
------
<xsl:for-each select="problem"><xsl:value-of select="solution"></xsl:for-each>
 
Old June 4th, 2006, 05:41 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>My problem is practically that I need to somehow use relative paths on the external document, or perhaps another approach. The reason is that I don't know how deep the menu is going to be, so the same "menu to ul", "link to a" and "title to <a>'s content" transformations should be applyed for each nested list.

This suggests to me that you need to a recursive depth-first traversal of the document using apply-templates rather than a simple selection using path expressions.

>If the XPath expression for inside the document is suppose to follow after the document() fucntion, then what's it's second argument for anyway?

If the first argument is a relative URI such as "lookup.xml", then the second argument can be used to change what it's taken as being relative to. By default, if the first argument is a string then it's relative to the stylesheet; if it's a node, then the URI is relative to that node. In practice it's very rare to use the second argument of document().

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with App root relative paths chroniclemaster1 ASP.NET 2.0 Basics 4 October 7th, 2008 03:34 AM
http: paths of document not found on mac rjonk XSLT 1 August 9th, 2006 03:42 PM
Virtual and relative paths - please explain SoC Classic ASP Basics 3 June 2nd, 2005 06:59 PM
Site root and document relative paths. nicnacs Dreamweaver (all versions) 5 October 5th, 2004 05:35 PM
HELP with relative paths in FileSystemObject stalker Javascript 5 September 4th, 2003 02:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.