Jump to content
Guest c2k2e

"value-of select=" syntax question

Recommended Posts

Guest c2k2e

I am having problems using a form that runs ASP code to generate an XML file that displays using an XSL file. I cannot find the syntax to use in the "value-of" statement for the format the XML is in. The XML tags are in the following heirarchy: DATA/FIELD/VALUE. Each VALUE tag has an ID attribute, this is what I am not able to display the text from in the XSL code. I would prefer not to use a for-each statement as each VALUE text entry needs to be formatted differently in the XSL (HTML).Here is my code:MAIN.XML

<?xml version="1.0" encoding="utf-8"?><?xml:stylesheet type='text/xsl' href='main.xsl'?><data>    <field>        <value id="date">11/25/2005</value>        <value id="audience">To All Personnel:</value>        <value id="message">There are no scheduled outages.</value>    </field></data>

MAIN.XSL (HTML removed for clarity)Problem with the xsl:value-of select="data/field/value" statement.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><xsl:value-of select="data/field/value" /><xsl:value-of select="data/field/value" /><xsl:value-of select="data/field/value" /></xsl:template></xsl:stylesheet>

SAVE.ASP

<%dim xmlDocdim rootEl,fieldName,fieldValue,attIDdim p,pi,i	'Do not stop if an error occursOn Error Resume NextSet xmlDoc = server.CreateObject("Microsoft.XMLDOM")xmlDoc.preserveWhiteSpace=true	'Create a root and field element and append it to the documentSet rootEl = xmlDoc.createElement("data")Set fieldName = xmlDoc.createElement("field")xmlDoc.appendChild rootElrootEl.appendChild fieldName	'Loop through the form collectionfor i = 1 To Request.Form.Count   'Eliminate button elements in the form  if instr(1,Request.Form.Key(i),"btn_")=0 then     'Create a value element and an id attribute    Set fieldValue = xmlDoc.createElement("value")    Set attID = xmlDoc.createAttribute("id")     'Set the value of the id attribute equal to the name of the current form field    attID.Text = Request.Form.Key(i)     'Append the id attribute to the field element    fieldValue.setAttributeNode attID     'Set the value of the value element equal to the value of the current form field    fieldValue.Text = Request.Form(i)     'Append the value element as a child of the field element    fieldName.appendChild fieldValue  end ifnext	'Add an XML processing instruction and insert it before the root elementSet p = xmlDoc.createProcessingInstruction("xml","version='1.0'")Set pi = xmlDoc.createProcessingInstruction("xml:stylesheet","type='text/xsl' href='main.xsl'")xmlDoc.insertBefore pi,xmlDoc.childNodes(0)xmlDoc.insertBefore p,xmlDoc.childNodes(0)	'Save the XML filexmlDoc.save (Server.Mappath("main.xml"))	'Release all object referencesset xmlDoc=nothingset rootEl=nothingset fieldName=nothingset fieldValue=nothingset attID=nothingset p=nothingset pi=nothing	'Test to see if an error occurredif err.number<>0 then  response.write("Error: No information saved.")else  response.write("Your information has been saved.")end if%>

FORM.HTM

<FORM ACTION="save.asp" METHOD="post"><p><b>Notification Date: </b><input type="text" name="date" size="43" tabindex="1" style="font-weight: 700"></font></p><p><b>Audience: </b><input type="text" name="audience" size="43" tabindex="2" style="font-weight: 700"></font></p><p><b>Message: </b><textarea name="message" rows="6" name="S1" cols="40" tabindex="3"></textarea></font></p><p><input type="reset" value="Reset" id="btn_res" name="btn_res" tabindex="4" style="width: 75">   <input type="submit" value="Submit" id="btn_sub" name="btn_sub" tabindex="5" style="width: 75"></p></FORM>

Let me know if this is not clear or if you need more info. i shouldn't have any coding errors, if it look slike it, it is because I accidentally removed something in this forum when removing the HTML. Right now everything works except it displays the first VALUE entry 3 times instead of each of the 3 individually (since I don't know how to specify the VALUE ID).Thanks!Chris

Share this post


Link to post
Share on other sites

To get a value of an attribute you use <xsl:value-of select="@attributename" />edit: or do you mean you want to use the attribute values in the xsl code so that all 3 of the value tags are displayed correctly and not just the first one 3 times? If so you can do this using XPath, specifically by identifying the position of the element you wish to extract the data from within its parent node. See http://www.w3schools.com/xpath/xpath_syntax.asp

Share this post


Link to post
Share on other sites

So in other words, I guess the XSL you need is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><xsl:value-of select="data/field/value[@date]" /><xsl:value-of select="data/field/value[@audience]" /><xsl:value-of select="data/field/value[@message]" /></xsl:template></xsl:stylesheet>

Share this post


Link to post
Share on other sites
So in other words, I guess the XSL you need is:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><xsl:value-of select="data/field/value[@date]" /><xsl:value-of select="data/field/value[@audience]" /><xsl:value-of select="data/field/value[@message]" /></xsl:template></xsl:stylesheet>

Or just this<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><xsl:value-of select="data/field/value/@date" /><xsl:value-of select="data/field/value/@audience" /><xsl:value-of select="data/field/value/@message" /></xsl:template></xsl:stylesheet>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...