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

Selecting entity value based on attribute value

Status
Not open for further replies.

tokind

Programmer
May 13, 2003
2
0
0
US
I'm stuck on something that should be simple. I have a "templet" of a form, and I am filling in the values of input fields with a dataset generated from an SQl query.

Code:
[XML]
<root>
 <template>
  <section>
   <line class=&quot;label&quot;>
    <edit type=&quot;TEXT&quot; label=&quot;First&quot; name=&quot;nameFirst&quot;/>
    <edit type=&quot;TEXT&quot; label=&quot;Last&quot; name=&quot;nameLast&quot;/>
   </line>
  <section>
  <!-- more sections -->
 </template>

 <dataSet>
   <nameFirst>Thomas</nameFirst>
   <nameLast>Smith</nameLast>
 </dataSet>
</root>
[/XML]

[XSLT]
<xslt:transform xmlns:xslt=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL] version=&quot;1.0&quot;>
<xslt:output method=&quot;html&quot; omit-xml-declaration=&quot;yes&quot;/>
 <xslt:template match=&quot;template&quot;>
  <xslt:apply-templates/>
 </xslt:template>

 <xslt:template match=&quot;section&quot;>
   <div class=&quot;formElement&quot;>
   <xslt:for-each select=&quot;line&quot;>
     <div class=&quot;line&quot;>
     <xslt:for-each select=&quot;edit&quot;>
       <span class=&quot;label&quot;>
       <xslt:value-of select=&quot;@label&quot;/>:</span>\n
       <xslt:element name=&quot;input&quot;>
	<xslt:attribute name=&quot;type&quot;>TEXT</xslt:attribute>
	<xslt:attribute name=&quot;name&quot;>
	  <xslt:apply-templates select=&quot;@name&quot;/>
	</xslt:attribute>
	<xslt:attribute name=&quot;class&quot;>
	  <xslt:apply-templates select=&quot;@class&quot;/>
	</xslt:attribute>
	<xslt:attribute name=&quot;value&quot;>

          <xslt:apply-templates select=&quot;//dataSet/@name&quot;/>

        </xslt:attribute>
      </xslt:element>
    </xslt:for-each>
    </div>
   </xslt:for-each>
   </div>
  </xslt:template>
  <xslt:template match=&quot;dataSet/*&quot;>
    <xslt:value-of select=&quot;.&quot;/>
  </xslt:template>
</xslt:transform>
[/XSLT]
[code]
I am trying to use the name e.g. nameFirst to select the value of nameFirst from my dataSet and assign that to the value= attribute of an element.

<xslt:apply-templates select=&quot;@name&quot;/>

 inserts &quot;nameFirst&quot; (being the name attribute) for value

<xslt:apply-templates select=&quot;//@name&quot;/>

 inserts all of the values of the members of dataSet

<xslt:apply-templates select=&quot;//dataSet/@name&quot;/>

 and many other variations, select nothing.

I have struggled with too many variations to number here. I would appreciate any pointers/
 
I think I can solve this for you but I do not exactly understand what you are trying to accomplish.

What would help (a lot) is to see the XML you start with and the XML (or whatever) you want to end up with.
The sentence
I am trying to use the name e.g. nameFirst to select the value of nameFirst from my dataSet and assign that to the value= attribute of an element.

puzzles me.

kib.at.kibje.com
 
K.O.

The xml is comprozed of a stored &quot;template that defines the contents and layout of the product, and the results of an sql query that comprses the information the product is displaying.
Code:
<form>
  <template>
    <title>My Profile</title>
    <para class=&quot;instructions&quot;> You may input your personal information here. When you have made the desired changes, click the Update Profile button at the bottom to save your information to the system.</para>
    <table name=&quot;People&quot; class=&quot;tableSet&quot;>
      <topic>Name</topic>
      <line class=&quot;label&quot;>
        <input type=&quot;TEXT&quot; label=&quot;First&quot; name=&quot;nameFirst&quot; class=&quot;FSA&quot;/>
        <input type=&quot;TEXT&quot; label=&quot;Last&quot; name=&quot;nameLast&quot; class=&quot;FSA&quot;/>
      </line>
      <line class=&quot;label&quot;>
        <nameC>
          <input type=&quot;TEXT&quot; label=&quot;Hon.&quot; name=&quot;Honorific&quot; class=&quot;FSA&quot; size=&quot;3&quot;/>
          <input type=&quot;TEXT&quot; label=&quot;Name&quot; name=&quot;Name&quot; class=&quot;FSA&quot; size=&quot;26&quot;/>
          <input type=&quot;TEXT&quot; label=&quot;Title&quot; name=&quot;Title&quot; class=&quot;FSA&quot; size=&quot;16&quot;/>
        </nameC>
      </line>
      <topic>Address</topic>
      <line class=&quot;label&quot;>
        <input type=&quot;TEXT&quot; label=&quot;Street&quot; name=&quot;address&quot; class=&quot;FSA&quot;/>, 
        <input type=&quot;TEXT&quot; name=&quot;city&quot; class=&quot;FSA&quot; size=&quot;12&quot;/>, 
        <input type=&quot;TEXT&quot; name=&quot;state&quot; class=&quot;FSA&quot; size=&quot;2&quot;/>
        <input type=&quot;TEXT&quot; name=&quot;ZIP&quot; class=&quot;FSA&quot; size=&quot;12&quot;/>
      </line>
      <line class=&quot;label&quot;>
        <input type=&quot;TEXT&quot; label=&quot;Email Address&quot; name=&quot;email&quot; class=&quot;FSA&quot;/>
        <input type=&quot;TEXT&quot; label=&quot;SSN (Private)&quot; name=&quot;SSN&quot; size=&quot;12&quot; class=&quot;FSA&quot;/>
      </line>
      <topic>Phones</topic>
      <line class=&quot;label&quot;>
        <phones>
          <input type=&quot;TEXT&quot; name=&quot;Location&quot; label=&quot;Location&quot; class=&quot;FSA&quot; size=&quot;6&quot;/>
          <input type=&quot;TEXT&quot; name=&quot;Number&quot; label=&quot;Number&quot; class=&quot;FSA&quot; size=&quot;14&quot;/>
        </phones><!-- input type=&quot;TEXT&quot; name=&quot;aLocation&quot; label=&quot;Location&quot; class=&quot;FSD&quot; size=&quot;6&quot;/>
<input type=&quot;TEXT&quot; name=&quot;aNumber&quot; label=&quot;Number&quot; class=&quot;FSA&quot; size=&quot;14&quot;/ -->
      </line>
    </table>
    <table name=&quot;Accounts&quot; class=&quot;tableSet&quot;>
      <topic>Account</topic>
      <line class=&quot;label&quot;>
        <input type=&quot;TEXT&quot; name=&quot;username&quot; label=&quot;Member ID&quot; disabled=&quot;DISABLED&quot; class=&quot;FSD&quot; size=&quot;18&quot;/>
        <input type=&quot;PASSWORD&quot; label=&quot;Password&quot; name=&quot;password&quot; class=&quot;FSA&quot;/>
      </line>
      <line class=&quot;label&quot;>
        <input type=&quot;SUBMIT&quot; name=&quot;action&quot; value=&quot;Update Profile&quot; checked=&quot;checked&quot; class=&quot;FSB&quot;/>
      </line>
    </table>
  </template>
  <sql>
    <username>toofriendly</username>
    <password>make8ice</password>
    <nameFirst>Frankie</nameFirst>
    <nameLast>Jones</nameLast>
    <nameC>
      <val name=&quot;Honorific&quot;>Dr.</val>
      <val name=&quot;Name&quot;>Franklin Samuel Jones</val>
      <val name=&quot;Title&quot;>PHD</val>
    </nameC>
    <SSN>nnn-nn-nnnn</SSN>
    <address>4276 Some St</address>
    <phoneC>
      <phone>
        <Location>Home</Location>
        <Number>505-555-1212</Number>
      </phone>
      <phone>
        <Location>Work</Location>
        <Number>505-644-1212</Number>
      </phone>
    </phoneC>
    <email>me@erg.com</email>
    <localityID>225</localityID>
    <city>Los Crevices</city>
    <county>Dana</county>
    <state>NM</state>
    <ZIP>88912</ZIP>
  </sql>
</form>

Note that &quot;form&quot; is the root, and the dataset is inserted as set &quot;sql&quot;.

The transform does layout duties like a stylsheet template, but must insert values in the form contents; values selected from the sql node set. I am populating a form with data.

The xslt:
Code:
<xslt:transform xmlns:xslt=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL] version=&quot;1.0&quot;>
  <xslt:output method=&quot;html&quot; omit-xml-declaration=&quot;yes&quot;/>
  <xslt:template match=&quot;template&quot;>
    <xslt:element name=&quot;form&quot;>
      <xslt:attribute name=&quot;method&quot;>POST</xslt:attribute>
      <xslt:apply-templates/>
    </xslt:element>
  </xslt:template><!-- begin segment processing -->
  <xslt:template match=&quot;table&quot;>
    <xslt:element name=&quot;div&quot;>
      <xslt:attribute name=&quot;name&quot;>
        <xslt:value-of select=&quot;@name&quot;/>
      </xslt:attribute>
      <xslt:attribute name=&quot;class&quot;>tableSet</xslt:attribute>
      <xslt:apply-templates/>
    </xslt:element>
  </xslt:template>
  <xslt:template match=&quot;line&quot;>
    <xslt:element name=&quot;div&quot;>
      <xslt:attribute name=&quot;class&quot;>line</xslt:attribute>
      <xslt:apply-templates/>
    </xslt:element>
  </xslt:template>
  <xslt:template match=&quot;set&quot;>
    <xslt:for-each select=&quot;set&quot;>
      <xslt:apply-templates/>
    </xslt:for-each>
  </xslt:template><!-- specific node processing -->
  <xslt:template match=&quot;sql&quot;><!-- returns nothing for data set values -->
  </xslt:template>
  <xslt:template match=&quot;title&quot;>
    <xslt:element name=&quot;h1&quot;>
      <xslt:value-of select=&quot;.&quot;/>
    </xslt:element>
  </xslt:template>
  <xslt:template match=&quot;para&quot;>
    <xslt:element name=&quot;p&quot;>
      <xslt:attribute name=&quot;class&quot;>
        <xslt:value-of select=&quot;@class&quot;/>
      </xslt:attribute>
      <xslt:value-of select=&quot;.&quot;/>
    </xslt:element>
  </xslt:template>
  <xslt:template match=&quot;topic&quot;>
    <xslt:element name=&quot;h2&quot;>
      <xslt:attribute name=&quot;class&quot;>topic</xslt:attribute>
      <xslt:value-of select=&quot;.&quot;/>
    </xslt:element>
  </xslt:template>
  <xslt:template match=&quot;line/input&quot;>
    <span class=&quot;label&quot;>
      <xslt:value-of select=&quot;@label&quot;/>:</span>
    <xslt:element name=&quot;input&quot;>
      <xslt:variable name=&quot;var&quot;>
        <xslt:value-of select=&quot;@name&quot;/>
      </xslt:variable>
      <xslt:attribute name=&quot;value&quot;>
        <xslt:value-of select=&quot;//sql/*[//line/input[$var]]&quot;/>
      </xslt:attribute>
      <xslt:call-template name=&quot;attribs&quot;/>
    </xslt:element>
  </xslt:template>
  <xslt:template match=&quot;nameC&quot;>
    <xslt:for-each select=&quot;input&quot;>
      <span class=&quot;label&quot;>
        <xslt:value-of select=&quot;@label&quot;/>:</span>
      <xslt:element name=&quot;input&quot;>
        <xslt:variable name=&quot;var&quot;>
          <xslt:value-of select=&quot;@name&quot;/>
        </xslt:variable>
        <xslt:attribute name=&quot;value&quot;>
          <xslt:value-of select=&quot;//nameC/val[@name=$var]&quot;/>
        </xslt:attribute>
        <xslt:call-template name=&quot;attribs&quot;/>
      </xslt:element>
    </xslt:for-each>
  </xslt:template>
  <xslt:template match=&quot;phones&quot;>
    <xslt:for-each select=&quot;//sql/phoneC/phone&quot;>
      <xslt:call-template name=&quot;phone&quot;/>
      <br/>
    </xslt:for-each>
  </xslt:template>
  <xslt:template name=&quot;phone&quot;>
    <xslt:for-each select=&quot;phones/input&quot;>
      <xslt:element name=&quot;span&quot;>
        <xslt:attribute name=&quot;class&quot;>
          <xslt:value-of select=&quot;@label&quot;/>
        </xslt:attribute>
      </xslt:element>\n 
      <xslt:element name=&quot;input&quot;>
        <xslt:variable name=&quot;var&quot;>
          <xslt:value-of select=&quot;//*/phone/.&quot;/>
        </xslt:variable>
        <xslt:attribute name=&quot;value&quot;>
          <xslt:value-of select=&quot;//phone/.&quot;/>
        </xslt:attribute>
        <xslt:attribute name=&quot;type&quot;>
          <xslt:value-of select=&quot;//*/phones/input/@type&quot;/>
        </xslt:attribute>
        <xslt:attribute name=&quot;name&quot;>
          <xslt:value-of select=&quot;@name&quot;/>
        </xslt:attribute>
        <xslt:attribute name=&quot;class&quot;>
          <xslt:value-of select=&quot;//*/phones/input/@class&quot;/>
        </xslt:attribute>
        <xslt:if test=&quot;//*/phones/input/@size&quot;>
          <xslt:attribute name=&quot;size&quot;>
            <xslt:value-of select=&quot;//*/phones/input/@size&quot;/>
          </xslt:attribute>
        </xslt:if>
      </xslt:element></xslt:for-each>
  </xslt:template><!-- attribs completes input node attributes for all input fileds -->
  <xslt:template name=&quot;attribs&quot;>
    <xslt:attribute name=&quot;type&quot;>
      <xslt:value-of select=&quot;@type&quot;/>
    </xslt:attribute>
    <xslt:attribute name=&quot;name&quot;>
      <xslt:value-of select=&quot;@name&quot;/>
    </xslt:attribute>
    <xslt:attribute name=&quot;class&quot;>
      <xslt:apply-templates select=&quot;@class&quot;/>
    </xslt:attribute>
    <xslt:if test=&quot;@size&quot;>
      <xslt:attribute name=&quot;size&quot;>
        <xslt:value-of select=&quot;@size&quot;/>
      </xslt:attribute>
    </xslt:if>
    <xslt:if test=&quot;@disabled&quot;>
      <xslt:attribute name=&quot;disabled&quot;>
        <xslt:value-of select=&quot;@disabled&quot;/>
      </xslt:attribute>
    </xslt:if>
  </xslt:template>
</xslt:transform>

Now here is the XHTML product.

Code:
<form method=&quot;POST&quot;>
<h1>My Profile</h1>
<p class=&quot;instructions&quot;> You may input your personal information here. When you have made the desired changes, click the Update Profile button at the bottom to save your information to the system.</p>
<div name=&quot;People&quot; class=&quot;tableSet&quot;>
<h2 class=&quot;topic&quot;>Name</h2>
<div class=&quot;line&quot;><span class=&quot;label&quot;>First:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;nameFirst&quot; class=&quot;FSA&quot;><span class=&quot;label&quot;>Last:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;nameLast&quot; class=&quot;FSA&quot;></div>
<div class=&quot;line&quot;><span class=&quot;label&quot;>Hon.:</span><input value=&quot;Dr.&quot; type=&quot;TEXT&quot; name=&quot;Honorific&quot; class=&quot;FSA&quot; size=&quot;3&quot;><span class=&quot;label&quot;>Name:</span><input value=&quot;Franklin Samuel Jones&quot; type=&quot;TEXT&quot; name=&quot;Name&quot; class=&quot;FSA&quot; size=&quot;26&quot;><span class=&quot;label&quot;>Title:</span><input value=&quot;PHD&quot; type=&quot;TEXT&quot; name=&quot;Title&quot; class=&quot;FSA&quot; size=&quot;16&quot;></div>
<h2 class=&quot;topic&quot;>Address</h2>
<div class=&quot;line&quot;><span class=&quot;label&quot;>Street:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;address&quot; class=&quot;FSA&quot;>, 
				<span class=&quot;label&quot;>:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;city&quot; class=&quot;FSA&quot; size=&quot;12&quot;>, 
				<span class=&quot;label&quot;>:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;state&quot; class=&quot;FSA&quot; size=&quot;2&quot;><span class=&quot;label&quot;>:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;ZIP&quot; class=&quot;FSA&quot; size=&quot;12&quot;></div>
<div class=&quot;line&quot;><span class=&quot;label&quot;>Email Address:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;email&quot; class=&quot;FSA&quot;><span class=&quot;label&quot;>SSN (Private):</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;SSN&quot; class=&quot;FSA&quot; size=&quot;12&quot;></div>
<h2 class=&quot;topic&quot;>Phones</h2>
<div class=&quot;line&quot;><br><br></div>
</div>
<div name=&quot;Accounts&quot; class=&quot;tableSet&quot;>
<h2 class=&quot;topic&quot;>Account</h2>
<div class=&quot;line&quot;><span class=&quot;label&quot;>Member ID:</span><input value=&quot;toofriendly&quot; type=&quot;TEXT&quot; name=&quot;username&quot; class=&quot;FSD&quot; size=&quot;18&quot; disabled><span class=&quot;label&quot;>Password:</span><input value=&quot;toofriendly&quot; type=&quot;PASSWORD&quot; name=&quot;password&quot; class=&quot;FSA&quot;></div>
<div class=&quot;line&quot;><span class=&quot;label&quot;>:</span><input value=&quot;toofriendly&quot; type=&quot;SUBMIT&quot; name=&quot;action&quot; class=&quot;FSB&quot;></div>
</div>
</form>

Instead of selecting a node from <sql> based on the @name of the <template> element, I am getting the FIRST element of the <sql> set with each select. I have tried various @node, node()=$var, and explicite selections like //sql/*[//template*/input/@name] to no avail.

I have managed to get something working by adding a name attribute to each <sql> node a la:
Code:
<sql>
  <username name=&quot;username&quot;>toofriendly</username>
  <password name=&quot;password&quot;>make8ice</password>
  <nameFirst name=&quot;nameFirst&quot;>Frankie</nameFirst>
  <nameLast name=&quot;nameLast&quot;>Jones</nameLast>
</sql>

and select on //sql/*[@name=$var]

which of course defeats the purpose of xml, huh?

Sorry about the language. I'm still getting the hang of it. I've only been doing this for two weeks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top