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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 4th, 2003, 12:20 AM
tokind's Avatar
tokind tokind is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: New Mexico
Posts: 31 tokind User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 55 sec
Reputation Power: 6
My brain and XSLT select statements

I've tried to pare this down to just the essentials... spent an increadable amount of time trying to figure out what my problem is.

Here is the XML.
Code:
<form>
	<template>
		<input type="TEXT" label="First" name="nameFirst" class="FSA"/>
		<input type="TEXT" label="Last" name="nameLast" class="FSA"/>
		<input type="TEXT" label="Street" name="address" class="FSA"/>, 
		<input type="TEXT" name="city" class="FSA" size="12"/>, 
		<input type="TEXT" name="state" class="FSA" size="2"/>
		<input type="TEXT" name="ZIP" class="FSA" size="12"/>
	</template>
	<sql>
		<nameFirst>Thomas</nameFirst>
		<nameLast>Kindig</nameLast>
		<address>4875 Beryl St</address>
		<city>LAS CRUCES</city>
		<state>NM</state>
		<ZIP>88012</ZIP>
	</sql>
</form>

Here is the transform.
Code:
<xslt:transform xmlns:xslt="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xslt:output method="html" omit-xml-declaration="yes"/>
	<xslt:template match="/">
		<xslt:element name="form">
			<xslt:attribute name="method">POST</xslt:attribute>
			<xslt:attribute name="class">form</xslt:attribute>\n
			<xslt:apply-templates/>
		</xslt:element>
	</xslt:template>

	<xslt:template match="input">
		<xslt:if test="@label">
			<span class="label">
			<xslt:value-of select="@label"/>:</span>
		</xslt:if>
		<xslt:element name="input">
			<xslt:attribute name="value">
				<xslt:apply-templates select="//sql/@name"/>
			</xslt:attribute>
			<xslt:call-template name="attribs"/>
		</xslt:element>\n
	</xslt:template>


	<xslt:template match="sql">
		<xslt:value-of select="."/>
	</xslt:template>

	<xslt:template name="attribs">
		<xslt:attribute name="type"><xslt:value-of select="@type"/></xslt:attribute>
		<xslt:attribute name="name"><xslt:value-of select="@name"/></xslt:attribute>
		<xslt:attribute name="class"><xslt:apply-templates select="@class"/></xslt:attribute>
		<xslt:if test="@size">
		 	<xslt:attribute name="size"><xslt:value-of select="@size"/></xslt:attribute>
		</xslt:if>
		<xslt:if test="@disabled">
			<xslt:attribute name="disabled"><xslt:value-of select="@disabled"/></xslt:attribute>
		</xslt:if>
	</xslt:template>
</xslt:transform>


Here is the problem.

As you can see, the <sql> node set is written after the <template> set. I extracted the <sql> set from a database and wrote it in there myself. Each <input> element corresponds to an <sql> child, identified by the @name of the <input>. But I cannot get this to select the value of the <sql><child:@name> node. I have tried everything I can think of. Two weeks!

<xslt:attribute name="value">
<xslt:value-of select="//sql/@name"/>
</xslt:attribute>

returns nothing.

<xslt:value-of select="@name"/>

returns the @name of the <input node> (duh!)

<xslt:value-of select="//@name"/>

returns ALL of the name elements from <template>

<xslt:apply-templates select="*all of the above*"/>

returns pretty much the same results.

I have also tried various combinations of

<xslt:value-of select="//sql/*[@name]"/>
and
<xslt:value-of select="//sql/*[node()=@name]"/>

which either select nothing, or return "false".

I obviously need a cluestick, if not a clue-by-four. Has anyone noticed that tutorials on these subjects are really pretty lame? Any suggestions?
__________________
Thomas Kindig
http://www.tokind.com
"We are as gods and might as well get good at it." -Stewart Brand

Last edited by tokind : June 4th, 2003 at 01:20 PM.

Reply With Quote
  #2  
Old June 4th, 2003, 10:02 AM
tokind's Avatar
tokind tokind is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: New Mexico
Posts: 31 tokind User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 55 sec
Reputation Power: 6
So this morning I am playing with switching the context of the select statement to the <sql> node set. It appears that I can do this, as
Code:
<xslt:template match="input">
   <span class="label">
      <xslt:value-of select="@label"/>:</span>
   <xslt:element name="input">
      <xslt:variable name="var">
         <xslt:value-of select="@name"/>
      </xslt:variable>
      <xslt:attribute name="value">
         <xslt:for-each select="//form/sql/*">
            <xslt:value-of select="."/>
         </xslt:for-each>
      </xslt:attribute>
</xslt:template>


I get a list of all of the //sql/* values for each select. I am in the right context!

But, if I add

Code:
<xslt:if test="//form/sql/node()=@name">
    <xslt:value-of select="."/>
</xslt:if>


to test for the specific element I am shooting for, I get nothing.

Maybe the test is wrong?

Reply With Quote
  #3  
Old June 4th, 2003, 11:51 AM
tokind's Avatar
tokind tokind is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: New Mexico
Posts: 31 tokind User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 20 m 55 sec
Reputation Power: 6
Thanks, the "art" (that is, the language) of this XML stuff keeps tripping me up. It's hard emough to read what I am doing, much less try to understand what someone else is doing. I am sorry if I am not clear here, trying again.

Put short: I have a template and a data set. I am populating the template with values from the dataset, and the output is an XHTML form.

I did get this function to work about 15 minutes ago.

Code:
<xslt:template match="input">
   <xslt:element name="input">
      <xslt:variable name="var">
         <xslt:value-of select="@name"/>
      </xslt:variable>
      <xslt:attribute name="value">
         <xslt:for-each select="//form/sql/*">
            <xslt:if test="name()=$var">
               <xslt:value-of select="."/>
            </xslt:if>
      </xslt:for-each>
   </xslt:attribute>
</xslt:template>


The trick was adding the for-each construct, even though I will only be selecting a single value (hard concept for a programer) so that I can then use if to select the specific node, who's name corresponds to the "name" attribute value of my field.

Yah; what do I mean "name attribute value"?

So, I am parsing template/input nodes. Then suddenly I need the contents of a //sql/n node, where n = the value of <input name

I switch contexts to //sql/* and test each node for name()=@name (which incidentally did not work without first setting a variable $var=@name then using name()=$var; I think this has to do with the context of the select).

I'm on to the next hurdle now. And late for the day job. Thanks. Gotta go.

Last edited by tokind : June 4th, 2003 at 01:17 PM.

Reply With Quote
  #4  
Old June 4th, 2003, 12:27 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
Catching up on threads you missed from the beginning is tough, but there are errors in the second line of your original xslt file.
Code:
<xslt<img src="images/smilies/redface.gif" border="0" alt="">utput method="html" omit-xml-declaration="yes"/>

That could possibly be tripping everything up, since who knows how the file is being parsed. Always pass xslt files through an xml validator, since an xslt is still valid xml. It can save you alot of headaches.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > My brain and XSLT select statements


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 5 hosted by Hostway