XML/XSL Values Not Updating

Posted on 2005-04-26
Last Modified: 2010-05-18
I have the below code that is generating no erros but not updating any values either.
What am I doing wrong?
                        myStr = "/root/packs/pack[PackID=" & arPack(Loopy) & "]"
                        response.Write myStr & " " & arPack(Loopy) & " = " & arPrice(Loopy) &  "<br>" '<== This line shows good data
                        response.Write oXML.selectSingleNode(myStr).getAttribute("DefaultCost") & " XX <br>"  '<== Blank

                        Call oRS2XML.setValue(oXML.selectSingleNode(myStr), "DefaultCost", arPrice(Loopy))
                        response.Write oXML.selectSingleNode(myStr).getAttribute("DefaultCost") & " XX <br>"  '<== Blank

(I can't change this subroutine)

      Public Sub setValue( parentNode, NodeName, NodeValue )
            If IsNull(NodeValue) then Exit Sub
            'response.write "," & NodeName & "," & NodeValue & "<br>"
            If gElementCentric Then
                  Dim newNode
                  Set newNode = parentNode.ownerDocument.createElement(NodeName)
                  newNode.text = nodeValue
                  Call parentNode.appendChild(newNode)
                  Set newNode = Nothing
                  Call parentNode.setAttribute(NodeName, NodeValue)
            End If
      End Sub
Question by:slamhound
    LVL 15

    Expert Comment

    Are you sure that the attribute "DefaultCost" actually has a value?? Maybe it's empty.

    Try this and see what's in the XML node:

    response.Write oXML.selectSingleNode(myStr).xml

    (You'll have to view the page source to view the output)
    LVL 10

    Author Comment

    I've added:
    response.Write oXML.selectSingleNode(myStr).xml & "~~"
    Call oRS2XML.setValue(oXML.selectSingleNode(myStr), "DefaultCost", arPrice(Loopy))
    response.Write oXML.selectSingleNode(myStr).xml & "~~"

    Both are comming up blank.
    LVL 15

    Expert Comment

    Are you sure? You'll have to view the page source to see the results because it should output an entire XML tag which your browser will ignore.

    I don't think it's possible for this to have no output. You're trying to write out the entire XML of a node. If the node doesn't exist then you'll get an error when you try and output it's .xml property. Assuming you're not getting any errors then there must some output.

    Make sure (for now anyway) you're not using any On Error Resume Next error catching.
    LVL 10

    Author Comment

    Sorry, I lost your output in all my debuggin output. Here is what you were looking for:

    <br>/root/packs/pack[PackID=29] 29 = 10<br> XX <br> <== My debugging stuff that shows we have packid 29 and that we have a value 10 that I want to insert into the DefaultCost.

    <PackName>Bag Tag Aeroplane</PackName>
    <PackGroupID>1</PackGroupID><PackGroupName>Bag Tags</PackGroupName>

    I'm looping through values so I grabbed what I thought was just the first set of information. You will notice duplicate information because the data is drawn from a query using inner joins and selected everything from both/all tables.

    I can't find any On Error stuff. I have inherited this code so can't be certain.
    LVL 15

    Accepted Solution

    OK, I think I understand your problem. Maybe...

    As far as I can tell the function setValue() is working correctly.

    The only thing that is wrong is your debug code :-)

    This line here is wrong:

    response.Write oXML.selectSingleNode(myStr).getAttribute("DefaultCost")

    This code has two parts: the selectSingleNode part and the getAttribute part

    What the first part does, based on your XPath query (ie. the value of myStr), is select a a <pack> node whose <PackId> child node has a value of 29.
    This selectSingleNode method returns an XML node object - the <pack> node.

    The second part (getAttribute) attempts to read the value of an attribute called "DefaultCost" from the node (ie. <pack>).
    But you can clearly see that <pack> has no atttributes, only child nodes.

    What you actually want to do is read the text property of the <DefaultCost> child node.
    The simplest way to do this is to change your XPath query to select the <DefaultCost> node then simply read it's text property:

    ' Change the XPath query to select the <DefaultCost> node
    myStr = "/root/packs/pack[PackID=" & arPack(Loopy) & "]/DefaultCost"
    ' Read the text property of this node.
    response.Write oXML.selectSingleNode(myStr).text

    LVL 10

    Author Comment

    Much better! Almost there!

    The value of DefaultCost is being added together like a string instead of being assinged the value

    Originally DefaultCost = 3.5
    I set DefaultCost to 10 and I get 3.510
    I set DefaultCost to 10 again I get 3.51010
    I set DefaultCost to 10 again I get 3.5101010

    How do I get Default cost to accpet the value rather than append it?
    LVL 15

    Expert Comment

    This must be happening somewhere else in your code but I can't see how that would be happening from the code you posted initially.

    My guess is that (somewhere) you're extracting the current value out of the node. This value will be a string. You're then using the "+" operator to add the new value. But because the value is a string the new value is appended rather than mathematically added. Confusingly VBScript lets you use either "&" or "+" as a string concatenator.
    LVL 10

    Author Comment

    Thanks Deighc. You have answered the question well. I still need more help so I've posted another question here that you may also be able to answer.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now