Skip to main content
rhpt
Known Participant
October 11, 2013
Question

Create i:nil XML Attribute

  • October 11, 2013
  • 1 reply
  • 1507 views

I need to create an XML Document with an element that requires the attribute i:nil=true. I've tried <cfset StructInsert(mydoc.employee.name.XmlAttributes, "i:nil", "true")>, but it returns the error "The right hand side of the assignment is not of type XML Node".

What I want is to create an element like this: <name i:nil="true"/>

Any help would be greatly appreciated.

This topic has been closed for replies.

1 reply

Inspiring
October 13, 2013

What does this produce?

<cfdump var="#isStruct( mydoc.employee.name.xmlAttributes )#" />

If it's Yes/True, I think the problem here has to do with you attempting to set a key with a value that is not permitted.  Variables cannot have special characters like ':' in them.  To quickly test this, change 'i:nil' to 'i_nil' since underscores are permitted.

The xmlAttributes() method returns back a structure of key/value pairs.

Is it valid for an XML node to look like:

<user lastname="smith" firstname="john" i:nil="true" /> ?

Not sure that's allowed.

If you're attempting to use namespaces, those are placed onto the root element <user> not its attributes.  Like:

<i:user lastname="smith" firstname="john" />

rhpt
rhptAuthor
Known Participant
October 14, 2013

Figured this out thank to Stackoverflow. Simply use a direct assignment.

<cfset mydoc.employee.name.xmlAttributes["i:nil"] = true>

Inspiring
October 14, 2013

Happy you got it rolling, but I gotta say I'm very surprised here.

To my knowledge the xmlAttributes() function returns a structure, which is just a series of name/value pairs.  I guess I'm so used to keeping special characters outside of my variable names that I never really tried to set one with a colon in it.  Come to think of it, I think CF is more concerned about the FIRST character of the variable name than subsequent ones.  Like, the variable couldn't be called ':nil', because it began with the colon, but the colon could exist as a subsequent character in the variable name.

Good to know.