Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 9th, 2005, 09:26 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default 2 XML Docs > 1 XSLT Stylesheet

I've posted this question before with no response, so I decided to simplify my explanation in hopes of making it easier to understand what I'm asking about:

I have 2 XML docs:
1) xmldoc1.xml - This file contains a central list of external links for our entire website. This is how it's set up:
<xmldoc1>
    <site id="site1">
        <url>http://www.site1.com/</url>
    </site>
    <site id="site2">
        <url>http://www.site2.com/</url>
    </site>
</xmldoc1>

2) xmldoc2.xml - This file contains a node named "link" that matches one of the "id" attributes from xmldoc1.xml. This is how it's set up:
<xmldoc2>
    <item>
        <title>SITE 1</title>
        <address>123 4th St.</address>
        <phone>(123) 456-7890</phone>
        <link>site1</link>
    </item>
    <item>
        <title>SITE 2</title>
        <address>987 6th St.</address>
        <phone>(987) 654-3210</phone>
        <link>site2</link>
    </item>
</xmldoc2>

And this is what I'd like the output to look like:

SITE 1
Address: 123 4th St.
Phone: (123) 456-7890
Website: http://www.site1.com/

SITE 2
Address: 987 6th St.
Phone: (987) 654-3210
Website: http://www.site2.com/

I know that I need an XSLT stylesheet that ties the 2 source XML docs together using the "id" attribute from xmldoc1.xml, and the "link" node from xmldoc2.xml, but I'm not sure how to accomplish this. I've been beating my head against a wall concerning what I thought would be a simple task, so I'm now hoping that some knowledgable folks can show me the light so-to-speak.

So if anyone can give me some insight in how to accomplish this task, it would be greatly appreciated!!! Thanks so much for your time.

KWilliams
Reply With Quote
  #2 (permalink)  
Old September 9th, 2005, 01:08 PM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

This should show you how to get started, run it against xmldoc2.xml.
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />
  <xsl:param name="linkDocPath" select="'xmldoc1.xml'" />
  <xsl:variable name="linkDoc" select="document($linkDocPath)" />
  <xsl:template match="/">
    <html>
      <head>
        <title>Sites</title>
      </head>
      <body>
        <xsl:apply-templates select="*/item" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <xsl:variable name="url" select="$linkDoc/*/site[@id = current()/link]/url" />
    <h3><xsl:value-of select="title" /></h3>
    <div>Address: <xsl:value-of select="address" /></div>
    <div>Phone: <xsl:value-of select="phone" /></div>
    <div>Web site: <a href="{$url}"><xsl:value-of select="$url" /></a></div>
  </xsl:template>

</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old September 9th, 2005, 01:49 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Joe,

Thanks for a quick, thorough, and correct answer to my question. It worked GREAT! You are a great asset to this forum:)

P.S.
I used this:
<xsl:variable name="linkDoc" select="document('xmldoc1.xml')" />

...instead of this:
<xsl:param name="linkDocPath" select="'xmldoc1.xml'" />
<xsl:variable name="linkDoc" select="document($linkDocPath)" />

...and it still worked great. Thanks.

KWilliams
Reply With Quote
  #4 (permalink)  
Old September 12th, 2005, 11:22 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:You are a great asset to this forum:)
Thanks

The reason I did it in two stages was that it's easier then to change the value of the XML file brought in via the document() function by externally setting the param named "linkDocPath".

--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formating <li> as per xml using xslt 1.0 tims XSLT 9 May 12th, 2008 01:30 AM
How do I transform XSLT without the <?xml?> tag? nadavvin XSLT 4 June 10th, 2007 09:25 AM
XML > XSLT and ASP.NET 2.0 bonekrusher XSLT 7 September 22nd, 2006 06:26 PM
Forms with XML > XSLT > XHTML kwilliams XSLT 0 October 27th, 2005 09:52 AM
Multiple XML Docs/One XSLT Stylesheet kwilliams XSLT 5 August 4th, 2005 11:54 AM



All times are GMT -4. The time now is 04:48 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.