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

Retreive only certain changing elements from xml with xsl 1

Status
Not open for further replies.

sullivans20

Technical User
May 16, 2007
3
US
I am having trouble trying to figure out how exactly I can grab different "cubes" with different ids on the fly so to speak. First here is my cube.xml file:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cube.xsl"?>
<office>
	<cube id="001">
		<empName>Removed</empName>
		<phoneNum>xxx</phoneNum>
		<machineName>Removed</machineName>
		<ip>Removed</ip>
	</cube>
	<cube id="002">
		<empName>Removed</empName>
		<phoneNum>xxx</phoneNum>
		<machineName>Removed</machineName>
		<ip>Removed</ip>
	</cube>
</office>
Now the above xml file would go all the way to 68 but I cut the rest out to save space. Now here is the xsl stylesheet i'm using to style the xml document (cube.xsl):
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>
  <body>
  	<xsl:for-each select="//cube[@id='001']">
    	<p align="center">
	    	<b>
	    		<u>
	    			Detailed Information For <xsl:value-of select="empName"/>
	    		</u>
	    	</b>
	    </p>
	    <br />
	    <table border="1">
	    	<tr bgcolor="#6183A6">
	  	       <th align="left">Employee Name</th>
	  	 	   <th align="left">Phone Number</th>
	  	 	   <th align="left">Machine Name</th>
	  	 	   <th align="left">IP Address</th>
	    	</tr>
     
 	   		<tr>
	    	  <td><xsl:value-of select="empName"/></td>
	    	  <td><xsl:value-of select="phoneNum"/></td>
	    	  <td><xsl:value-of select="machineName"/></td>
	    	  <td><xsl:value-of select="ip"/></td>
	    	</tr>
	    </table>
     </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
Here is the page i'm calling the xml file from (you can ignore the second xml file troubleLog.xml for now in the second frame):
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html>
	<head>
		<title>Floor Plan - Cube One</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="icon" href="favicon.ico" type="image/x-icon" />
		<link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon" /> 
	</head>       
	<frameset border="1" cols="375,*" frameBorder="0" frameSpacing="4">
	<noframes>
		<body>
		</body>
	</noframes>
		<frame name="right" src="cube.xml">
		<frame name="left" src="troubleLog.xml">
	</frameset>
</html>
As you can see right now I have cube.xsl hard coded to only retrieve records matching cube="001". This is no good because I might want to retreive any other record 1-68 in cube.xml.

Let me try and give you a bigger picture to try and help you better understand what i'm trying to do. On my main page I have a huge graphic of a office floor. When the user clicks on a cubicle it will bring up information about the employee who sits there. It will also have a trouble shooting log for each person (thats what troubleLog.xml is for). All this will be determined though by what cube a user clicks on.

If a user clicks cube 2 they should only get the information returned to them matching cube id="002". My problem right now is figuring out how exactly I can do this. I'm not sure exactly how I can accomplish this. I know I could do it if I made a new xml and xsl file for each cube but this seems like a really dirty hack (not to mention super tedious). If anyone has an idea i'd love to hear it.
 
Thanks for the help. I tried to follow your advice and now my cube.xml file looks like this:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cube.xsl"?>
<?nmode 1?>
<office>
    <cube id="001">
        <empName>Removed</empName>
        <phoneNum>xxx</phoneNum>
        <machineName>Removed</machineName>
        <ip>Removed</ip>
    </cube>
    <cube id="002">
        <empName>Removed</empName>
        <phoneNum>xxx</phoneNum>
        <machineName>Removed</machineName>
        <ip>Removed</ip>
    </cube>
</office>
As you can see I added <?nmode 1?> to the file for testing purposes. I also made modifications to my cube.xsl file:
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:param name="nmode">
    <xsl:value-of select="(//processing-instruction('//cube[@id=001]')[name()='nmode'])[1]" />
    
    <xsl:template match="/">

	<html>
  	<body>
      	<p align="center">
	    	<b>
	    		<u>
	    			Detailed Information For <xsl:value-of select="empName"/>
	    		</u>
	    	</b>
	    </p>
	    <br />
	    <table border="1">
	    	<tr bgcolor="#6183A6">
	  	       <th align="left">Employee Name</th>
	  	 	   <th align="left">Phone Number</th>
	  	 	   <th align="left">Machine Name</th>
	  	 	   <th align="left">IP Address</th>
	    	</tr>
     
 	   		<tr>
	    	  <td><xsl:value-of select="empName"/></td>
	    	  <td><xsl:value-of select="phoneNum"/></td>
	    	  <td><xsl:value-of select="machineName"/></td>
	    	  <td><xsl:value-of select="ip"/></td>
	    	</tr>
	    </table>
    </body>
	</html>
	</xsl:template>
</xsl:param>
</xsl:stylesheet>
I think everything is working because now when i open cube.xml I just get a blank screen. I think this is because I am just linking directly to cube.xml without giving it any parameters. Is there anyway I could specify the parameter to pass to cube.xml in a link in HTML? Or do I have to use javascript to do this? If I have to use javascript do you have any good code samples?

Thanks.
 
That seems to be an insufficient understanding to what discussed there. It is very far off the point that I would exhaust me just to pointing them out.

In any case, you have to redesign the mechanics of the page. The iframe src should point to a server-side page with some querystring (?id=xxx). That part is set by some client-side scripting of some event handler.

The src page should be a page [1] taking in the querystring("id") into a server-side variable. [2] Then the page would call loading the xml source document and xsl document with [3] the parameter set by the querystring. Then [4] the transformation is done with the ingredients now all ready, namely, xml & xsl & parameters. [5] The output is then sent to the iframe.

The setting up of the parameter looks like this.
[tt]
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="[blue]<xsl:param name="s_id" />[/blue]
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="//cube[@id=[blue]$s_id[/blue]]">
<p align="center">
<b>
<u>
Detailed Information For <xsl:value-of select="empName"/>
</u>
</b>
</p>
<br />
<table border="1">
<tr bgcolor="#6183A6">
<th align="left">Employee Name</th>
<th align="left">Phone Number</th>
<th align="left">Machine Name</th>
<th align="left">IP Address</th>
</tr>

<tr>
<td><xsl:value-of select="empName"/></td>
<td><xsl:value-of select="phoneNum"/></td>
<td><xsl:value-of select="machineName"/></td>
<td><xsl:value-of select="ip"/></td>
</tr>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
[/tt]
The parameter s_id is empty in the xsl document. But before the transformation takes place, it is set to the querystring id by the server-side script.

That is the layout of the program to realize this functionality. It is not small task if you are not familiar with any one part of sub-task.
 
Thank you for the detailed response. This makes a whole lot more sense now. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top