Jump to content

Increment


xmlxpert
 Share

Recommended Posts

Hi, How do I increment the value by one on the last record (the child ID) in appending a new record in my xml file? But before that, how do I know its the last record? I'm using asp and xsl.data.xml

<product>               <On sale>	                   <id>1</id>                   <name>Television</name>                   <description>14 " HiFi</description>                   <dateadded>apr 6, 2006</dataadded>                </On sale>                <On sale>                        <id>2</id>                     <name>Washing</name>                    <description>rotor max</description>                    <dateadded>apr 7, 2006</dataadded>                </On sale></product>

Link to comment
Share on other sites

XSLT does have the generate-id() function thoguh. However, it's actually used like a kind of a temporary variable, rather then a identifier. So if you want to generate something to be displayed, it's truly better so generate it with ASP.There's also one other way though. By using the position() function. However, the "id" will not be preserved if the position is changed.

Link to comment
Share on other sites

If you are using id as an element and you can write ASP then have a look at the XMLDOM tutorial.I think if you use the MAX() function to get the highest ID value and then use script to add one to this value then you can store the new value as the ID of the new element.I'm going to be doing something like this with my current project so I'd be happy to compare notes when I've got it working.Dooberry. :)

Link to comment
Share on other sites

:) Ok, I don't think this is the best way of doing it, but I used java to create a session variable which will hold the total number of records
var no_of_records

Then I've created a button to add records

<input type="button" onclick="AddNewNode()" id="addrecord" value="Add New Record" />

I then used the following script to edit the xml file:

function AddNewNode(){  var xmldoc  var myelement  var newnode  var subnode  xmldoc = new ActiveXObject("Microsoft.XMLDOM")  xmldoc.async="false"  xmldoc.load("myxmldoc.xml")  myelement = xmldoc.documentElement  newnode = xmldoc.createElement("On Sale")  subnode = xmldoc.createElement("id")  subnode.appendChild(xmldoc.createTextNode(String(no_of_records+1)))  newnode.appendChild(subnode)  myelement.appendChild(newnode)  xmldoc.save("myxmldoc.xml")}

This did work with my project although I'm sure there are tidier ways to do this than to have a session global variable. Hopefully someone will look at this and tell me that I'm still a rookie and show me where the code can be improved (68 posts does not a master make!!).

It's all fun and games till someone loses an "i", then someone else has to go through all the code and figure out where the typo is - pah!!
Dooberry :)
Link to comment
Share on other sites

I think I've found a better way of doing this,

function AddNewNode(){ var xmldoc var myelement var newnode var subnode xmldoc = new ActiveXObject("Microsoft.XMLDOM") xmldoc.async="false" xmldoc.load("myxmldoc.xml") myelement = xmldoc.documentElement newnode = xmldoc.createElement("On Sale") subnode = xmldoc.createElement("id") subnode.appendChild(xmldoc.createTextNode(String(myelement.childNodes.length +1))) newnode.appendChild(subnode) myelement.appendChild(newnode) xmldoc.save("myxmldoc.xml")}

The alteration is minor, but significant in that there is now no need for a session variable and there is no need to update the session variable when you add records because the length property changes as you update the file.This also means that there are no problems if you forget to update the session variable!!.I'm not so green as I'm cabbage-looking (as we say in these parts). :)Dooberry.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...