Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pull info from multiple XML files where IDs match 2

Status
Not open for further replies.

artguy

Technical User
Feb 2, 2001
117
0
0
US
I have 2 xml files that I'm trying to combine to display some contact info.

My goal is to grab the Department info (address, phone) from the dept.xml file and add a list of contacts that are located in the corresponding dept from the contact.xml file by matching the ID field in one file to the dept field in the other.

I'm learning to use XSL to parse the XML files as I go along with success up till this point. Using ASP, if it matters. Not sure if I'm going about it the right way even.

I know I'll somehow need to use the document() function but am not sure how to do the comparison and be able to grab multiple contacts that have the same dept ID number. I was thinking I may somehow have to use 2 XSL files but that is a guess.

Thank you for your time. Any help/guidance is appreciated.

Here is the code:
dept-list.xml
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]

<xsl:template match="/">
      <xsl:for-each select="categories/dept">
      <xsl:sort select="deptname"/>

   <h2><xsl:value-of select="deptname"/></h2>
        <p><xsl:value-of select="address1"/><br />
        <xsl:value-of select="city"/>, <xsl:value-of select="state"/> <xsl:value-of select="zip"/> </p>
          <p> <xsl:value-of select="phone"/> &bull; Fax: <xsl:value-of select="fax"/></p>

          <ul class="nobullets">
            <li> <a href="EMAIL HERE">FIRST LAST</a> TITLE</li> 'Pull Info from contact.xml where dept field matches ID field
            <li> <a href="EMAIL HERE">FIRST LAST</a> TITLE</li>
            <li> <a href="EMAIL HERE">FIRST LAST</a> TITLE</li>
          </ul>

      </xsl:for-each>

</xsl:template>
</xsl:stylesheet>

contacts.xml
Code:
<?xml version="1.0"?>
   <contacts>
	<contact>
	   <id>20051123003018</id>
	      <first>Jack</first>
	      <last>Frost</last>
	      <title>Snow Man</title>
	      <email>snow@snow.edu</email>
	      <dept>20051122232558</dept>   'corresponds with ID in dept.xml
	</contact>
        ...

dept.xml
Code:
<?xml version="1.0"?>
<categories>
  <dept>
     <id>20051122232558</id>
     <deptname>Flake Dept</deptname>
     <address1>123 Smith Street</address1>
     <address2></address2>
     <suitefloor></suitefloor>
     <city>JS</city>
     <state>NY</state>
     <zip>000000</zip>
     <phone></phone>
     <fax></fax>
  </dept>
        ...
 
I would do it like this:
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title>Department Contacts</title>
      </head>
      <body>
        <xsl:apply-templates select="categories/dept">
          <xsl:sort select="deptname"/>
        </xsl:apply-templates>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="dept">
    <div>
      <h2>
        <xsl:value-of select="deptname"/>
      </h2>
      <p>
        <xsl:value-of select="address1"/>
        <br/>
        <xsl:value-of select="city"/>, <xsl:value-of select="state"/>
        <xsl:value-of select="zip"/>
      </p>
      <p>
        <xsl:value-of select="phone"/> &bull; Fax: <xsl:value-of select="fax"/>
      </p>
      <ul class="nobullets">
        <xsl:for-each select="document('contacts.xml')/contacts/contact">
          <li>
            <a href="{email}">
              <xsl:value-of select="concat(first, ' ', last)"/>
            </a>
            <xsl:value-of select="title"/>
          </li>
        </xsl:for-each>
      </ul>
    </div>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Thank you for your response! That solution is very close to what I need. The only thing is it displays all contacts under every dept. instead of only where the fields I mentioned match. (see examples below)

Is there a way to accomplish this?

This is what is displays:
Code:
<div>
<h2>Snow Department</h2>
<p>123 Smith Street<br>JS, NY 00000</p>
<p>(111) 555-5785 - Fax: (111) 555-5786</p>
<ul class="nobullets">
[COLOR=red]<li><a href="mailto:snow@snow.edu">Jack Frost</a> -
            Snow Man</li>
<li><a href="mailto:sflake@snow.edu">Suzy Snowflake</a> -
            Civil Engineer</li>[/color red]
</ul>
</div>
<div>
<h2>South Pole Station</h2>
<p>US Tower<br>Anchorage, AK 99999</p>
<p>(000) 867-5309 - Fax: (000) 867-5310</p>
<ul class="nobullets">
[COLOR=red]<li><a href="mailto:snow@snow.edu">Jack Frost</a> -
            Snow Man</li>
<li><a href="mailto:sflake@snow.edu">Suzy Snowflake</a> -
            Civil Engineer</li>[/color red]
</ul>
</div>

This is what I'd like to display:
Code:
<div>
<h2>Snow Department</h2>
<p>123 Smith Street<br>JS, NY 00000</p>
<p>(111) 555-5785 - Fax: (111) 555-5786</p>
<ul class="nobullets">
[COLOR=red]<li><a href="mailto:snow@snow.edu">Jack Frost</a> -
            Snow Man</li>[/color red]
</ul>
</div>
<div>
<h2>South Pole Station</h2>
<p>US Tower<br>Anchorage, AK 99999</p>
<p>(000) 867-5309 - Fax: (000) 867-5310</p>
<ul class="nobullets">
[COLOR=red]<li><a href="mailto:sflake@snow.edu">Suzy Snowflake</a> -
            Civil Engineer</li>[/color red]
</ul>
</div>
 
Oops, forgot about that bit. Change this line:
Code:
<xsl:for-each select="document('contacts.xml')/contacts/contact[id = current()/id]">code]

Jon

"I don't regret this, but I both rue and lament it.
 
Perfect! I did change the code to be:

<xsl:for-each select="document('contacts.xml')/contacts/contact[dept = current()/id]">

since the ID I need to match in the contacts file is "dept" but that's all. Just my weird naming convention.

Thank you very much for your time and knowledge! Using XSL (XSLT) is very handy, once you figure out how each function works.

Thanks again, Jon!
 
Good stuff Jon, I'm looking at some ideas which will benefit significantly from this kind of power i.e. being able to fuse together data from different source xml files.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top