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

Cannot change attribute value

Status
Not open for further replies.

RickBeddoe

Programmer
Nov 18, 2008
32
0
0
US
Hello Folks,

I am having a hard time changing the attribute value in the XML file below. I am using XML in a VBA environment.

I have the following XML data:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns:link=" <sub xmlns:attr="foo">
</sub>
</root>

I would like to change the value of the 'xmlns:attr' attribute in the 'sub' element. When I use the code below, I get the error "Attempt to modify a read-only node".

If I remove the xmlns prefix from the attribute, it works. Any suggestions?


Set testNode = doc.getElementsByTagName("sub").Item(0) testNode.Attributes.getNamedItem("xmlns:attr").nodeValue = "fi
 
no can do. The namespace prefix is part of the XML file I'm processing. The XML file is generated from an external application. The namespace prefix needs to be here.
 
that's not it since I am able to change attribute values that do not have a namespace prefix.
 
Maybe that's not a reason as by deleting the namespace it's not inheriting it. Did you have a look at the schema?

Cheers,
Dian
 
Now that I think ... is that a valid XML?

Shouldn't it be

Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns:link="[URL unfurl="true"]http://foo.com">[/URL]
    <sub link:attr="foo">
    </sub>
</root>
?

Cheers,
Dian
 
I don't think that's it since I am able to change attribute values that do not have a namespace prefix.
 
sorry, posted that last one before your reply.

you are right about the valid xml and I think that was the problem.

I was able to get it to work.

Sometimes it helps to have a second pair of eyeballs look at it. Especially when it's damn allergy season.

*sneeze*
 
well...

I've been trying so many things that I've now returned to what the actual problem was. Here's the xml:

<root xmlns=" xmlns:link=" <sub link:attr="foo">
</sub>
</root>

If I try to change the 'xmlns:link' attribute, I get the read-only error mentioned above.

As for the schema, I'm not using one for this test. Therefore, I'm wondering if the default settings for any root element (regardless of the name) is to make it read-only.
 
well, this is lame, but I know it will work.

I will have to read in the XML attribute then take a sledgehammer, open the XML file up as a text file, and just replace the text that way.

That's what I was doing before, but it takes a few pages of code vs. the one or two lines I was hoping to use.

oh well.

:-(
 
Well, you're manipulating a DOM and trying to change de default namespace ... that's like manipulating a text and try to change the language. The exception are you getting is totally logical.

Have you considered an XSLT transformation?

Cheers,
Dian
 
I should probably give a little back story here.

I have a set of XML "templates" (not XSL templates) that I am using as a foundation for what we call a DTS (Discoverably Taxonomy Set) for building XBRL (Extensible Buisiness Reporting Language) financial reports. All I need to do is copy these templates into a working directory and set the appropriate attributes to match the parameters for the spefic DTS. There is one schema (taxonomy) file and 5 supporting XML files (linkbases) which reference the schema file. Likewise, the schema file has tags which reference the linkbases.

I could try using XSLT, but that's too much work for the little bit I'mt trying to accomplish. It's probably apparent that I'm no expert with XML.

As for changing the attribute, the one I'm trying to change is not the default namespace. In fact, I created an attribute named 'stuff' and was not able to change the value for that one either. Interesting that this is only in the Root element where I'm having this problem. I can have this attribute in a child node and change it without a problem. In addition, this only seems to be an issue with the DOM in VBA. I can do this in C# without a problem. I could build a .dll and call that, but I'm more than halfway into the woods building this in VBA. It seems there is some default behavior in the VBA DOM which sets the root node to read-only.

Thanks for the input. For now I'm moving on to other areas of this application. I've got it working with the 'brute force' method.

Cheers,

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top