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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML/XSLT and Logic

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
GB
Hi

This is probably a daft question but i'm gonna ask it anyway.

Is it possible to mix XSLT with data access logic?

What i'm trying to do is read an xml file, use values from one element to lookup another value in a database and then replace the original xml element value with the one found in the database.

For example:
<?xml version="1.0" encoding="utf-8"?>
<Vehicles>
<Vehicle>
<DealerID>2719</DealerID>
</Vehicle>
</Vehicles>

In the xml above, I want to be able to extract the DealerID 2719, look in the database to find a corresponding value (say 3087) and replace the value in the xml file.

The resulting xml file would like like the following:
<?xml version="1.0" encoding="utf-8"?>
<Vehicles>
<Vehicle>
<DealerID>3087</DealerID>
</Vehicle>
</Vehicles>

Any help/comments appreciated.

Smeat
 
something like this...?
Code:
<xsl:template match="/">
[b]<Vehicles>[/b]
   <xsl:for-each select="[b]//Vehicle"[/b]>
     [b]<Vehicle>
        <DealerID>[/b]
           <xsl:variable name="[b]Search[/b]" select="[b]DealerID[/b]" />
           <xsl:variable name="[b]Result[/b]" select="document([b]'Lookup.xml')/IDs/ID[Find=$Search]/Find[/b]"/>
           <xsl:if test="[b]$Result"[/b]><xsl:value-of select="[b]$Result[/b]" /></xsl:if>
           <xsl:if test="[b]not($Result)[/b]"><xsl:value-of select="[b]DealerID[/b]" /></xsl:if>
        [b]</DealerID>
     </Vehicle>[/b]
   </xsl:for-each>
[b]</Vehicles>[/b]
</xsl:template>

Where the replace db looked like this:
Lookup.xml
Code:
<IDs>
  <ID>
    <Find>2719</Find>
    <Replace>3087</Replace>
  </ID>
  <ID>
    <Find>1234</Find>
    <Replace>2345</Replace>
  </ID>
</IDs>

That should at least point you in the right direction, if not do the trick ;-)

Let me know if you need anything explained...

Hope this helps :)
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top