xmlxpert Posted April 7, 2006 Share Posted April 7, 2006 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 More sharing options...
Little Goat Posted April 7, 2006 Share Posted April 7, 2006 what is it you want to increment? Link to comment Share on other sites More sharing options...
xmlxpert Posted April 7, 2006 Author Share Posted April 7, 2006 what is it you want to increment?<{POST_SNAPBACK}> Hi, Oh sorry, I just want to increment the value of <id> child everytime a new record is added. Link to comment Share on other sites More sharing options...
Little Goat Posted April 7, 2006 Share Posted April 7, 2006 well, xsl cant change the file, but asp probably could. not sure how...LG Link to comment Share on other sites More sharing options...
boen_robot Posted April 7, 2006 Share Posted April 7, 2006 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 More sharing options...
dooberry Posted April 11, 2006 Share Posted April 11, 2006 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 More sharing options...
dooberry Posted April 12, 2006 Share Posted April 12, 2006 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 recordsvar 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 More sharing options...
dooberry Posted April 19, 2006 Share Posted April 19, 2006 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now