XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 4th, 2004, 09:59 AM
eflanigan eflanigan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 5 eflanigan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Passing variables through URL

I am just learning XML and I want to use it as a database. I know there are many other alternatives, but I need to do it this way.

I have an XML file acting as a DB. The filename is airportList.xml and it is included below:

------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="airportList_Filter.xsl"?>

<airportinfo>
<airport>
<code>BWI</code>
<name>Baltimore Washington International</name>
<ticketopen>4:00 a.m.</ticketopen>
<ticketclose>9:00 p.m.</ticketclose>
<curbsidecheckin>Available</curbsidecheckin>
</airport>

<airport>
<code>SCE</code>
<name>State College Airport</name>
<ticketopen>5:00 a.m.</ticketopen>
<ticketclose>8:30 p.m.</ticketclose>
<curbsidecheckin>Not Available</curbsidecheckin>
</airport>
</airportinfo>
------------------------------------------


I also have an XSL file formatting the output. It's filename is airportList_Filter.xsl and it is included below:

------------------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<html>
<body>

Airport List
<table>
<xsl:for-each select="airportinfo/airport[code='BWI']">
<br>Name: <xsl:value-of select="name"/></br>
<br>Ticket Counter Opens: <xsl:value-of select="ticketopen"/></br>
</xsl:for-each>
</table>

</body>
</html>
</xsl:template>
</xsl:stylesheet>
------------------------------------------

In the XSL I want the line:
<xsl:for-each select="airportinfo/airport[code='BWI']">
to read:
<xsl:for-each select="airportinfo/airport[code=VARIABLENAME]">

So that I can have a URL pass the variable of the airport code such as:
http://mydomain.com/airportList.xml?airportcode=BWI

And the page will only display information for BWI (or the other hundreds of airports)

Can anyone help? Thanks! Sorry for the indenting thing - this forum took it all away!

- Eric

Reply With Quote
  #2  
Old June 4th, 2004, 10:22 AM
nihaarika2002 nihaarika2002 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 49 nihaarika2002 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
passing variable(parameter)

hi
i hope this is the one what u r looking for

u can pass parameter value like this for this parameter
<xslaram name="airportID" select="'SCE'"/>

==========================

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslaram name="airportID" select="'SCE'"/>
<xsl:template match="/">

<html>
<body>

Airport List
<table>
<xsl:for-each select="airportinfo/airport[code=$airportID]">
<br>Name: <xsl:value-of select="name"/></br>
<br>Ticket Counter Opens: <xsl:value-of select="ticketopen"/></br>
</xsl:for-each>
</table>

</body>
</html>
</xsl:template>
</xsl:stylesheet>


==========================
hope this helps
with regards
niha

Reply With Quote
  #3  
Old June 4th, 2004, 10:46 AM
noahwesley noahwesley is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 5 noahwesley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation

This is basically the same question I posted yesterday. I understand using the paramater, but how do you pass the URL 'querystring' variable into the XSL file to dynamically change that paramater (and thus, dynamically filter)?

That's what I want to know.

Thanks.

~Noah~

Reply With Quote
  #4  
Old June 4th, 2004, 10:46 AM
eflanigan eflanigan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 5 eflanigan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Close!

niha -

That's close! But, I would still need a different XSL file for each airport code. I would like the airportID variable to get it's value from the URL

<xsl:Param name="airportID" select="'SCE'"/>

I want it to look something like this:

<xsl:Param name="airportID" select=GETVALUEFROMURL/>

when the URL looks like this:

http://mdomain.com/airportList.xml?airportID=BWI

Anymore help would be appreciated! Thanks!

EDIT: I found another message board with the same question. There is an answer that works here, but it is waaaay too much code and I have tried to manupilate it and if i delete just one line, it all goes wack.

http://www.experts-exchange.com/Web...Q_20310939.html


- Eric

Reply With Quote
  #5  
Old June 10th, 2004, 04:50 PM
krisbeyers krisbeyers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 6 krisbeyers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Client-side Solution that also works in Netscape

Previous solution won't work in Netscape, because it uses a Microsoft JScript in the XSL file.
You would normally use param, but then you need a server-side parser.

*** See next message for a better solution ***

I made following solution.. that works in IE and Netscape:
Just add new nodes to a loaded xml object on the client-side.
For each variable you received in your URL via document.location.search (do some string splitting) you create a new node like <variable><name>my_var_name</name><value>my_var_value</value></variable> and create a root <variables> containing all submitted variable nodes. Then add this root as a child of the root of your xml file.

Then load the xml object again, and your xsl can now use the URL variables like any other xml data.

ps: also used the domwrapper from http://www.webreference.com/program...ipt/domwrapper/ Because Netscape hasn't the loadXML function.

Greetings,
Kris

<html>
<head>
<script type="text/javascript" src="nczXMLDOMWrapper.js"></script>
<script type="text/javascript">
var xmlDoc;
var xmlVars;
var xsl;

// plaats de url variabelen in een xml structuur om ze in xsl te kunnen gebruiken
var text = new String("<variabelen>");
if(document.location.search!="") {
var variabelen = document.location.search.substring(1).split("&");
var variabele;
for(var i=0;i<variabelen.length;i++) {
variabele = variabelen[i].split("=");

text += "<" + variabele[0] + ">";
text += variabele[1];
text += "</" + variabele[0] + ">";
}
}
text += "</variabelen>";

// def de xml bestand xmlDoc
if (window.ActiveXObject) {
xmlDoc = jsXML.createDOMDocument(); // def
xmlDoc.async = false;
xmlDoc.onreadystatechange = function(){if(xmlDoc.readyState == 4){pop_ticker();}}
} else
if (document.implementation && document.implementation.createDocument) {
xmlDoc = jsXML.createDOMDocument(); // document.implementation.createDocument("","xmlDoc",null);
xmlDoc.onload = function(){pop_ticker();}
}

// laad xml bestand xmlDoc
xmlDoc.load("advertenties.xml");

function pop_ticker() {
if(window.ActiveXObject) {
xmlVars = jsXML.createDOMDocument(); // definieer xmlVars asynchroon
xmlVars.async = false;
xsl = jsXML.createDOMDocument(); // definieer xsl asynchroon
xsl.async = false;

xmlVars.loadXML(text); // stop de url variabelen in een dom object
var rootVars = xmlVars.documentElement; // vraag een verwijzing naar de nodes
var rootVars_node = rootVars.cloneNode(true); // maak een kopie om deze aan een tak van een ander dom object te kunnen hangen
xmlDoc.documentElement.appendChild(rootVars_node); // voeg de root variablen van xmlVars aan de root adds van xmlDoc toe

xsl.load("merge.xsl"); // laad het xsl bestand
document.write(xmlDoc.transformNode(xsl)); // pas xsl toe
} else {
objValidXMLString = jsXML.createDOMDocument(); // maak een DOM wrapper om de missende functie loadxml in Netscape te voorzien
objValidXMLString.loadXML(text); // stop de url variabelen in een dom object
var rootVars = objValidXMLString.documentElement; // vraag een verwijzing naar de nodes
var rootVars_node = rootVars.cloneNode(true); // maak een kopie om deze aan een tak van een ander dom object te kunnen hangen
xmlDoc.documentElement.appendChild(rootVars_node); // voeg de root variablen van xmlVars aan de root adds van xmlDoc toe

var processor = new XSLTProcessor(); // start xsl parser
var myXMLHTTPRequest = new XMLHttpRequest(); // open een http kanaal om de informatie binnen te halen zonder de browser te refreshen
myXMLHTTPRequest.open("GET", "merge.xsl", false); // stuur http request, false=assynchroon
myXMLHTTPRequest.send(null); // onbekend

var xslRef = myXMLHTTPRequest.responseXML; // haal xsl binnen
processor.importStylesheet(xslRef); // koppel xsl bestand aan xsl parser
var fragment = processor.transformToFragment(xmlDoc, document); // doe de transfomatie op het xml document

document.getElementById("xmlTemplate").innerHTML = ""; // Netscape kan geen volledige pagina vernieuwen voor xsl, wel in een div layer
document.getElementById("xmlTemplate").appendChild(fragment); // toon het fragment in de div layer
}
}
</script>
</head>
<body>
<DIV id="xmlTemplate"> </DIV>
</body>
</html>

Reply With Quote
  #6  
Old June 12th, 2004, 06:48 PM
krisbeyers krisbeyers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 6 krisbeyers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile The perfect solution :P

Hoi,

I noted that i need to use IE6.0+ specific elements like the "variable" and "with-param" XSL element, which are nescessary if you want to query with a selected value via XPath. So to go around this I rewrote my script, using only DOM to query the XML data.

Thanks to that wonderful Sarissa wrapper it also works in Netscape and others, I really love it!!

Sarissa:
http://sarissa.sourceforge.net (documentation)
https://sourceforge.net/projects/sarissa (the Sarissa files)

The script
<html>
<head>
<script type="text/javascript" src="Sarissa.js"></script>
<script type="text/javascript">
////////////////////////////////////////////////////////
// IE 5.0+ of NS 6.0+
////////////////////////////////////////////////////////
var xmlDoc;
var strZoekAdds = "";
var tblZoekAdds = new Array(); // om join te kunnen toepassen
var url_variabele;

////////////////////////////////////////////////////////
// vraag de url variabelen met prefix zoek_adds_ op en plaats ze in een XPath zoek string
////////////////////////////////////////////////////////
var z=0;

if(document.location.search!="") {
var url_variabelen = document.location.search.substring(1).split("&");

for(var v=0; v<url_variabelen.length; v++) {
url_variabele = url_variabelen[v].split("=");
if(url_variabele[0].substring(0,10)=="zoek_adds_")
tblZoekAdds[z++] = url_variabele[0].substring(10) + "='" + url_variabele[1] + "'";
};
};
strZoekAdds = tblZoekAdds.join(" and ");
if(tblZoekAdds.length)
strZoekAdds = "[" + strZoekAdds + "]";
strZoekAdds = "//adds/add" + strZoekAdds;

////////////////////////////////////////////////////////
// def de bestand met bron adressen
////////////////////////////////////////////////////////

if (window.ActiveXObject) {
xmlDoc = Sarissa.getDomDocument(); // def
xmlDoc.async = false;
xmlDoc.onreadystatechange = function(){if(xmlDoc.readyState == 4){pop_ticker();}};
} else
if (document.implementation && document.implementation.createDocument) {
xmlDoc = Sarissa.getDomDocument(); // document.implementation.createDocument("","xmlDoc",null);
xmlDoc.onload = function(){pop_ticker();};
};

////////////////////////////////////////////////////////
// laad bestand met bron adressen
////////////////////////////////////////////////////////
xmlDoc.load("advertenties.xml");

////////////////////////////////////////////////////////
// bestand met bron adressen is geladen (event)
////////////////////////////////////////////////////////
function pop_ticker() {
////////////////////////////////////////////////////////
// bronnen, zoekcountry en zoekgender zijn gekend
// laad bronnen in een tabel
////////////////////////////////////////////////////////

var xmlData = Sarissa.getDomDocument();
var xmlRoot = Sarissa.getDomDocument();
var xmlAdds = Sarissa.getDomDocument();
var xmlError = Sarissa.getDomDocument();
var xmlAddsFiltered = Sarissa.getDomDocument();
var xslDoc;
xmlData.async = false;
xmlRoot.async = false;
xmlAdds.async = false;
xmlError.async = false;
xmlAddsFiltered.async = false;

var adres = "";
var xmlnotfound = "";
var rootVars;
var rootVars_node;

xmlAdds.loadXML("<adds/>");
xmlRoot.loadXML("<root/>");

var loopAdressen = xmlDoc.getElementsByTagName("file");

xmlnotfound = "<xmlnotfound>";
for (var i=0; i<loopAdressen.length; i++) {
adres = loopAdressen.item(i).firstChild.nodeValue;
try {
xmlData.load(adres);
rootVars = xmlData.documentElement; // voeg data uit xmlData toe aan xmlDoc
rootVars_node = rootVars.cloneNode(true);
xmlAdds.documentElement.appendChild(rootVars_node);
} catch (e) {
xmlnotfound += "<address>" + adres + "</address>";
};
};
xmlnotfound += "</xmlnotfound>";

xmlError.loadXML(xmlnotfound);
rootVars = xmlError.documentElement;
rootVars_node = rootVars.cloneNode(true);
xmlRoot.documentElement.appendChild(rootVars_node);

////////////////////////////////////////////////////////
// bronnen zijn ingeladen in xmlRoot, sorteer en/of filter
////////////////////////////////////////////////////////

// selectNodes niet ondersteund in NS, opl. met for lussen


rootVars_node = xmlAdds.selectNodes(strZoekAdds);

xmlAddsFiltered.loadXML("<adds/>");
for(var j=0; j<rootVars_node.length; j++) {
hulp = rootVars_node.item(j).cloneNode(true);
xmlAddsFiltered.documentElement.appendChild(hulp);
};

rootVars = xmlAddsFiltered.documentElement;
rootVars_node = rootVars.cloneNode(true);
xmlRoot.documentElement.appendChild(rootVars_node);

////////////////////////////////////////////////////////
// bronnen zijn ingeladen en gesorteerd en/of gefilterd in xmlRoot, roep nu de xsl op
////////////////////////////////////////////////////////

// alert(xmlRoot.xml);

if(window.ActiveXObject) {
xslDoc = new ActiveXObject("Microsoft.XMLDOM"); // def
xslDoc.async = false;
xslDoc.load("merge.xsl"); // laad het xsl bestand
document.write(xmlRoot.transformNode(xslDoc).replace(/\n/g,"<br />"));
} else {
var processor = new XSLTProcessor(); // start xsl parser
var myXMLHTTPRequest = new XMLHttpRequest(); // open een http kanaal om de informatie binnen te halen zonder de browser te refreshen (IE 5.0+)
myXMLHTTPRequest.open("GET", "merge.xsl", false); // stuur http request, false=assynchroon
myXMLHTTPRequest.send(null); // onbekend

var xslRef = myXMLHTTPRequest.responseXML; // haal xsl binnen
processor.importStylesheet(xslRef); // koppel xsl bestand aan xsl parser
var fragment = processor.transformToFragment(xmlRoot, document); // doe de transfomatie op het xml document
document.getElementById("xmlTemplate").innerHTML = ""; // Netscape kan geen volledige pagina vernieuwen voor xsl, wel in een div layer
document.getElementById("xmlTemplate").appendChild(fragment); // toon het fragment in de div layer
document.getElementById("xmlTemplate").innerHTML = document.getElementById("xmlTemplate").innerHTML.replace(/\n/g,"<br />");
};
}
</script>
</head>
<body>
<DIV id="xmlTemplate" style="width: 200px; height: 200px;"> </DIV>
</body>
</html>

Reply With Quote
  #7  
Old June 16th, 2004, 11:00 AM
sammydafish sammydafish is offline
<?=php_geek(:p)?>
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Upstate NY
Posts: 69 sammydafish User rank is Lance Corporal (50 - 100 Reputation Level)sammydafish User rank is Lance Corporal (50 - 100 Reputation Level)sammydafish User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 10 h 49 m 40 sec
Reputation Power: 5
why not do it on the server side. You could use somethng like PHP. Just make sure PHP parses xsl files and do something like this

PHP Code:
<xsl:Param name="airportID" select=<?=$_GET['airportid']?>/> 

Reply With Quote
  #8  
Old June 18th, 2004, 02:34 PM
krisbeyers krisbeyers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 6 krisbeyers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Passing variables through URL

that solution i know too, but the idea was to make a distributed system where not all websites are stored on a server with server side scripting.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Passing variables through URL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT