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 September 9th, 2005, 09:26 AM
Banned
 
Join Date: Jul 2005
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
 
Old September 9th, 2005, 01:08 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old September 9th, 2005, 01:49 PM
Banned
 
Join Date: Jul 2005
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
 
Old September 12th, 2005, 11:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)





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





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